简体   繁体   中英

iOS UITableView with dynamic prototypes and custom cell

I have uitableviewcontroller with tableview with dynamic prototypes cell and even I change my cell style to subtitle or something else or I add accessory my cell always shows as basic style cell.

Here is my cellForRowAtIndexPath function:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (_dataArray && row < self.dataArray.count) {
        cell.textLabel.text = self.dataArray[row];
    }

    return cell;
}

EDIT: There is something I don't understand, maybe someone can explain this. Earlier my app crashed when I show my table view controller, so I had to add this to the viewDidLoad method:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];

After that the app did not crash, but it caused that I could not change the cell style. Now I can delete that, my app don't crash and I can change the cell style. Why?

What am I doing wrong?

Make sure you set the identifier of the cell in storyboard.

在此处输入图片说明

Also use tags to modifier UI elements from storyboard:

UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
UILabel *eventNameLabel = (UILabel *)[cell viewWithTag:2];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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