Loading a Reusable UITableViewCell from a Nib

前端 未结 16 2221
遇见更好的自我
遇见更好的自我 2020-11-27 09:59

I am able to design custom UITableViewCells and load them just fine using the technique described in the thread found at http://forums.macrumors.com/showthread.php?t=545061.

16条回答
  •  感情败类
    2020-11-27 10:26

    I can't remember where I found this code originally, but it's been working great for me so far.

    - (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"CustomTableCell";
        static NSString *CellNib = @"CustomTableCellView";
    
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
            cell = (UITableViewCell *)[nib objectAtIndex:0];
        }
    
        // perform additional custom work...
    
        return cell;
    }
    

    Example Interface Builder setup...

提交回复
热议问题