开发者

UITableViewCell not displaying data

开发者 https://www.devze.com 2023-01-19 12:53 出处:网络
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil)
    {
        //cell = [[[CustomCell alloc] initwithidentifier:CustomCellIdentifier] autorelease];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id oneObject in nib)
            if ([oneObject isKindOfClass:[CustomCell class]]) {
                cell = (CustomCell *)oneObject;
            }
    }

    NSUInteger row = [indexPath row];
    NSDictionary *rowData = [self.listData objectAtIndex:row];
    cell.nameLabel.text = @"text";
    [cell.contentView addSubview: nameLabel];
    [cell reloadInputViews];开发者_C百科

    return cell;
}

But the nameLabel wont display with its new name. What am I doing wrong please?? Thanks!


I'm willing to guess that if you inspect cell.NameLabel, you'll find that it's nil — the IBOutlet hasn't been wired up correctly by loadNibNamed:owner:options:. Are you sure that CustomCell has nameLabel as an appropriate outlet (ie, one that can be reached by key-value coding)? Apple's reference for that is here, but ensuring you have a setter and getter is sufficient.

As you've created your NIB, you're probably right that CustomCell isn't the file owner? If it is then loadNibNamed:owner:options: obviously shouldn't be called on self. And if 'self' is the actual owner, it may be neater to have an IBOutlet (also definitely KVC compliant) that points to the CustomCellView rather than fishing through the returned array.

ADDITIONS, subsequent to the comment discussion below:

What definitely works is to create a custom subclass of UITableViewCell with suitable IBOutlets that are also properties, to set your table view data source as the file owner and to use loadNib to load a new instance of the NIB with your class as the target, causing your outlet to have a new instance attached. Then you can use the properties of the subclass to access various subviews of the cell. Sample project: here.

0

精彩评论

暂无评论...
验证码 换一张
取 消