繁体   English   中英

表格视图未填充对象

[英]Table View not populating objects

- (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相当陌生,但是我不确定我的错误在代码中。 有人可以帮我吗?

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

  • 您尚未设置表视图的dataSource;
  • 您没有实现numberOfSectionsInTableView:和/或tableView:numberOfRowsInSection:回调。

如果要让单元出队,则需要在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;
}

像这样更改代码,您的代码将运行,

 - (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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM