Loading a Reusable UITableViewCell from a Nib

前端 未结 16 2112
遇见更好的自我
遇见更好的自我 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:28

    As of iOS circa 4.0, there are specific instructions in the iOS docs that make this work super-fast:

    http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7

    Scroll down to where it talks about subclassing UITableViewCell.

    0 讨论(0)
  • 2020-11-27 10:32

    Now, in iOS 5 there is an appropriate UITableView method for that:

    - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
    
    0 讨论(0)
  • 2020-11-27 10:33
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *simpleTableIdentifier = @"CustomCell";
    
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }         
    
    return cell;
    }
    
    0 讨论(0)
  • 2020-11-27 10:37

    The gustavogb solution doesn't work for me, what I tried is :

    ChainesController *c = [[ChainesController alloc] initWithNibName:@"ChainesController" bundle:nil];
    [[NSBundle mainBundle] loadNibNamed:@"ChaineArticleCell" owner:c options:nil];
    cell = [c.blogTableViewCell retain];
    [c release];
    

    It seems to work. The blogTableViewCell is the IBOutlet for the cell and ChainesController is the file's owner.

    0 讨论(0)
提交回复
热议问题