2011年6月5日 星期日

Custom Table View Cell

  • Command+N: Cocoa Touch -> Object-C class -> UITableViewCell -> [ClassName]
  • Command+N: User Interface -> Empty -> [ClassNameForNib]
  • 可以在ClassNameForNib裡面排版這個Cell要怎麼表示。並連結ClassName裡的IBOutlet


修改 ViewController的  - (UITableViewCell *)tableView: cellForRowAtIndexPath:

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

    }


這是因為再還沒有可再利用的顯示格時,他會先從nib載入。以上的nib會回傳一個這個nib內所有的物件陣列。由於Apple並無標準文件告知會由何種順序出現,而且也一直再改變,不得已只好先依此來找到Cell的表示方式。

若有改變到高度的話,就要修改


- (CGFloat)tableView:heightForRowAtIndexPath: 

沒有留言:

張貼留言