使用Nib自定義Cell的複用

自定義的單元格如下:


NewFriendCell.h 文件中對應的輸出口 :

#import <UIKit/UIKit.h>

@interface NewFriendCell : UITableViewCell

@property (retain, nonatomic) IBOutlet UIImageView *imgHead;
@property (retain, nonatomic) IBOutlet UILabel *labUserName;
@property (retain, nonatomic) IBOutlet UILabel *labState;

@end

1). option+command+4 打開屬性檢查器設置Cell 的identifier (這個Identifier要和程序中的reuseIdentifier 保持一致),這裏設置爲:CellTableIdentifier


2). TableView註冊自定義的Cell的Nib文件

// 定義一個和Cell中identifier中一致的字符串,複用和註冊Nib用到
static NSString* cellId = @"CellTableIdentifier";//identifier 和Nib文件中的要保持一致



// 註冊自定義Cell的自定義方法
-(void)initTableView{
    UINib* nib = [UINib nibWithNibName:@"NewFriendCell" bundle:nil];
    [self.myTableView registerNib:nib forCellReuseIdentifier:cellId];
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     //…… 
    [self initTableView];
    
}

3). 複用使用自定義的Cell 需要用到第一步中的identifier

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewFriendCell* cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    AddFriendRecoreds* record = newlyFriens[indexPath.row];
    cell.labUserName.text = record.userName;//用戶名
    //狀態
    if (record.addFriendType == AddFriendTypeAddOther) {
        if (record.addFriendState == AddFriendStateProcessing) {
            cell.labState.text = @"等待驗證";
        }else if(record.addFriendState == AddFriendStateSuccess) {
            cell.labState.text = @"已添加";
        }
    }else if (record.addFriendType == AddFriendTypeRequireAdd){
        if (record.addFriendState == AddFriendStateProcessing) {
            cell.labState.text = @"待接受";
        }else if(record.addFriendState == AddFriendStateSuccess) {
            cell.labState.text = @"已添加";
        }
    }
    return  cell;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章