表视图不填充对象

时间:2014-01-23 14:40:03

标签: ios arrays uitableview

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

    GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GenusNameCell"];


        cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    }

    return cell;
}

我有一个带有数组对象的tableview,但是当我运行它时。什么都没有出现。我对xcode很新,但我不确定我的错误在代码中是什么。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

如果从未调用cellForRowAtIndexPath回调,则可能是:

  • 您没有设置tableview的dataSource;
  • 你没有实现numberOfSectionsInTableView:和/或tableView:numberOfRowsInSection:callbacks。

如果您将单元格出列,则需要在if分支之外设置文本:

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

    GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GenusNameCell"];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];

    return cell;
}

答案 1 :(得分:0)

更改代码将运行的代码,

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

static NSString *CellIdentifier = @"GenusNameCell";

GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GenusNameCell"];

    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

}
cell.GenusNameLabel.text = [[genusName objectAtIndex:indexPath.row] geniusname];

return cell;
}

geniusname是您存储此人姓名的NSString。

相关问题