IOS UITableview 的Cell 封裝

感覺不錯的代碼。收藏下。

#import "GDBookTableViewCell.h"
#import "GDBook.h"

@interface GDBookTableViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iconLabel;
@property (weak, nonatomic) IBOutlet UILabel *authorLabel;
@property (weak, nonatomic) IBOutlet UILabel *booknameLabel;
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;

@end

@implementation GDBookTableViewCell

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"bookCell";
    
    GDBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"GDBookTableViewCell" owner:nil options:nil] lastObject];
    }
    
    return cell;
}

-(void)setBook:(GDBook *)book
{
    _book = book;
    
    self.booknameLabel.text = book.name;
}

@end

調用的地方:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GDBookTableViewCell *cell = [GDBookTableViewCell cellWithTableView:tableView];
    
    // 設置數據
    GDBook *book = self.books[indexPath.row];
    cell.book = book;
    
    return cell;
}

然後在
setBook裏實現對控件的綁定。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章