0

我正在使用 TableView 来显示一组数据,但是当我编译应用程序时,似乎没有调用 TableView 的函数。下面是我为此目的使用的代码:

- (NSInteger)tableView:(UITableView *)TableView numberOfRowsInSection:(NSInteger)section {

    return listdata.count;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"myCell"];    
    if (cell == nil) {
        cell =  [[ UITableViewCell alloc ] initWithFrame:CGRectZero];
    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [self.listdata objectAtIndex:row];

    return cell;
}
4

1 回答 1

1

If you're nto using a UITableViewController but simply adding a UITableView in a UIViewController, you need to set the delegate and datasource of your TableView to the VC's class.

You can do this in your storyboard, by ctrl-dragging from the TableView to the ViewController and choosing delegate/datasource, or you can do it programmatically by putting this code in the ViewController's .m, in the viewDidLoad method :

_tableView.delegate = self;
_tableView.datasource = self;

and add <UITableViewDelegate, UITableViewDatasource> to your ViewController's .h :

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDatasource>
于 2012-12-10T15:43:10.407 回答