2010-07-26 58 views
22

Tôi muốn đặt câu hỏi về UITableView của mục tiêu C. Tôi đang viết một chương trình và tôi muốn tạo giao diện người dùng theo lập trình. Tuy nhiên, tôi không biết cách hiển thị bảng theo lập trình. Tôi đã có một NSMutableArray để lưu trữ dữ liệu được hiển thị. Và tôi tạo một đối tượng UITableView *tableData;, tôi nên làm gì cho bước tiếp theo? Cảm ơn nhiều.Làm thế nào để hiển thị UITableView theo chương trình?

Trả lời

38

sau khi viết mã trên, bạn phải triển khai phương thức ủy nhiệm của UITableView.

đây là những phương pháp đại biểu của tải UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [your array count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    } 

    // Configure the cell... 
    cell.textLabel.text = [yourarray objectAtIndex:indexPath.row]; 

    return cell; 

} 
+0

cảm ơn bạn đã trả lời. Nhưng tôi không sử dụng lớp UIViewTable, tôi chỉ sử dụng lớp đơn giản. Bạn có thể cho tôi biết làm thế nào để thêm mảng vào bảng hoặc ô bảng? Cảm ơn bạn. – Questions

+0

@MarkSiu không cần phải triển khai lớp UITableView chỉ cần viết mã để thêm UITableView vào hàm viewdidload của bạn và trên ba hàm hiển thị bảng trong khung nhìn của bạn – priyanka

+0

cảm ơn bạn đã trả lời. Tôi đã thêm các hàm trên vào lớp .m. Tuy nhiên, tôi thấy rằng những chức năng đó không được gọi bởi UITableView. Khi nào các chức năng được gọi? Cảm ơn bạn. – Questions

43

Something như thế này nên làm các trick (giả sử này đang chạy từ bên trong bộ điều khiển xem của bạn và bạn có một tài sản thiết lập cho xem bảng):

tableView = [[[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStylePlain] autorelease]; 
tableView.dataSource = self; 
tableView.delegate = self; 

[self.view addSubview:tableView]; 

đâu bạn thay thế CGRectMake(...) với bất cứ vị trí/size bạn muốn.

+0

cảm ơn bạn đã trả lời. Nhưng tôi muốn hỏi, tôi nên đặt NSMutableArray ở đâu? cảm ơn bạn. – Questions

+0

Bạn sẽ lưu trữ mảng trong bộ điều khiển xem của bạn và sử dụng nó trong các phương thức UITableViewDataSource. Ví dụ, bạn sẽ trả về độ dài của mảng trong tableView: numberOfRowsInSection :. –

+0

cảm ơn bạn đã trả lời. Bởi vì giao diện người dùng chứa các phần tử khác, tôi thêm UITableView như một phần của chúng. Và tôi không biết cách gọi UITableDataSource vì lớp không phải là lớp xem bảng khi tôi tạo nó. Bạn có muốn cho tôi biết cách nhập hoặc thêm dữ liệu trong mảng không? cảm ơn bạn. – Questions

21
  • Tạo một lớp mới được thừa kế từ UIViewController.
  • Làm cho phù hợp với giao thức UITableViewDataSource.
  • Khai báo chế độ xem bảng của bạn.

tập tin tiêu đề của bạn nên được như sau:

@interface MyViewController : UIViewController <UITableViewDataSource> {  

} 

@property (nonatomic, retain) UITableView *tableView; 

@end 

Trong phương pháp viewLoad của lớp học của bạn:

  • Tạo một cái nhìn bảng sử dụng initWithFrame. Sử dụng kích thước 320x460 cho chiều cao đầy đủ. Xóa 44 khỏi chiều cao nếu bạn có thanh điều hướng và 49 nếu bạn có thanh tab.
  • Tạo chế độ xem mới.
  • Thêm chế độ xem bảng vào chế độ xem mới.
  • Đặt chế độ xem bộ điều khiển thành chế độ xem mới.
  • Đặt nguồn dữ liệu chế độ xem bảng cho cá thể của bạn (tự).
  • Thực hiện hai phương pháp nguồn dữ liệu cần thiết: tableView: cellForRowAtIndexPath và tableView: numberOfRowsInSection

tập tin thực thi của bạn nên được như sau:

#import "MyViewController.h" 

@implementation MyViewController 

@synthesize tableView=_tableView; 

- (void)dealloc 
{ 
    [_tableView release]; 

    [super dealloc]; 
} 

#pragma mark - View lifecycle 

- (void)loadView 
{ 
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0) style:UITableViewStylePlain]; 
    self.tableView = tableView; 
    [tableView release];  

    UIView *view = [[UIView alloc] init]; 
    [view addSubview:self.tableView]; 
    self.view = view; 
    [view release]; 

    self.tableView.dataSource = self; 
} 

- (void)viewDidUnload { 
    self.tableView = nil; 

    [super viewDidUnload]; 
} 

#pragma mark - Table view data source 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyCellIdentifier = @"MyCellIdentifier"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier]; 

    if(cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier] autorelease]; 
    } 

    return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 5; 
} 

@end 
2

Có lẽ nó cũng hữu ích cho tất cả những ai mới trong bạn đó.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // init table view 
    tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 

    //or, you may do that 
    //tableView = [[UITableView alloc] init]; 
    //tableView.frame = CGRectMake:(5 , 5 , 320 , 300); 

    // must set delegate & dataSource, otherwise the the table will be empty and not responsive 
    tableView.delegate = self; 
    tableView.dataSource = self; 

    tableView.backgroundColor = [UIColor cyanColor]; 

    // add to canvas 
    [self.view addSubview:tableView]; 
} 

#pragma mark - UITableViewDataSource 
// number of section(s), now I assume there is only 1 section 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView 
{ 
    return 1; 
} 

// number of row in the section, I assume there is only 1 row 
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 

// the cell will be returned to the tableView 
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"HistoryCell"; 

    // Similar to UITableViewCell, but 
    JSCustomCell *cell = (JSCustomCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[JSCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 
    // Just want to test, so I hardcode the data 
    cell.descriptionLabel.text = @"Testing"; 

    return cell; 
} 

@end 
+0

cho những người mới (tôi), để sao chép/dán vào một ứng dụng chung, thay đổi 'JSCustomCell' thành 'UITableViewCell', thay đổi 'cell.descriptionLabel.text' thành 'cell.textLabel.text'. số đếm ngược của các mục mảng của bạn cho 'numberOfRowsInSection'. nhắc nhở để khai báo các phương thức ủy nhiệm (tôi nghĩ đó là những gì nó được gọi) trong tệp .h, ''. (cảm ơn bạn Abhay) – tmr

Các vấn đề liên quan