2014年9月14日 星期日

New a custom UITableViewCell

New a custom UITableViewCell

1. New Xib -> choose Empty.

2. Drag UITableViewCell and give a identifier.

3. Drag elements you want.

4. New file -> Objective-C class and inherit UITableViewCell.

5. Back to Xib.

6. Edit Custom class (same with the new Objective-C class).
must has
- (id) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
    
        
    }
    return self;

}

7. Connect xib elements to .m IBOutlet.

8. Go to xxxUITableViewController.m

9. (Only have .m) In ViewDidLoad:      [YourTableView registerClass:[YourTableViewCell class] forCellReuseIdentifier:CellIdentifier];

10. (With xib) In ViewDidLoad:   [YourTableView registerNib:[UINib nibWithNibName:@"YourTableViewCell's name" bundle:nil]
         forCellReuseIdentifier:CellIdentifier];

11. In    cellForRowAtIndexPath:   
YourTableViewCell *cell = (YourTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
        cell = (YourTableViewCell *)[nib objectAtIndex:0];
    }

12. Good job and try.









沒有留言:

張貼留言