自定義單元格(IOS) – iPhone手機開發技術文章 iPhone軟體開發教學課程

自定義單元格有三種方法

- 代碼實現

- xib

- storyboard(推薦)

在故事板中操作方法為

1、在TableView屬性的Prototype Cells設置為1,默認為1;

2、需要創建自定義的單元格類;

3、設定Table View Cell的Class為自定義類;

自定義類:(並不難)

#import "CustomCell.h"

@implementation CustomCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

必須實現的數據源協議的方法:

#pragma mark --UITableViewDataSource 協議方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.listTeams count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 定義可重用單元格對象
    static NSString *cellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    NSUInteger row = [indexPath row];
    NSDictionary *rowDict = [self.listTeams objectAtIndex:row];
    cell.name.text = [rowDict objectForKey:@"name"];
 
    NSString *imagePath = [rowDict objectForKey:@"image"];
    imagePath = [imagePath stringByAppendingString:@".png"];
    cell.image.image = [UIImage imageNamed:imagePath];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

發佈留言