简体   繁体   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;
}

I have a tableview with an Array of objects but when I run it. 我有一个带有对象数组的tableview,但是当我运行它时。 Nothing shows up. 什么都没有出现。 Im fairly new to xcode but Im not sure what my mistake is in the code. 我对xcode相当陌生,但是我不确定我的错误在代码中。 Can some one help me out? 有人可以帮我吗?

If the cellForRowAtIndexPath callback is never called, it could be: 如果从未调用cellForRowAtIndexPath回调,则可能是:

  • you didn't have set the dataSource of your tableview; 您尚未设置表视图的dataSource;
  • you didn't implement the numberOfSectionsInTableView: and/or tableView:numberOfRowsInSection: callbacks. 您没有实现numberOfSectionsInTableView:和/或tableView:numberOfRowsInSection:回调。

And if you dequeue your cell, you need to set the text outside the if branch like this: 如果要让单元出队,则需要在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;
}

Change the code like this your code will run, 像这样更改代码,您的代码将运行,

 - (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 is the NSString you stored the name of the person. geniusname是您存储的人名的NSString。

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

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