好久没有用过xib作为cell来使用了,今天使用了一下,改了好多地方才好,赶紧记录一下,下次直接看看。
第一步 创建一个空的xib
第二步拖进一个tableViewcell 进来
第三步 创建一个tableViewcell的类文件,然后关联起来。
第四步写代码
其中有两种做法
1 注册的方法在viewdidLoad中写入注册
// 注册cell
[self.tableViewShow registerNib:[UINib nibWithNibName:@"settingSwitchCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CellIdentifier"];
然后在UItableVIewCell的方法中
SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
return (UITableViewCell *)cell;
第二种是和其他xib一样
直接在UItableVIewCell的方法中
static NSString * Identifier2 =@"Cell2";
if (!cell) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"settingSwitchCell" owner:nil options:nil];
cell = (SettingTableViewCell *)[nibArray objectAtIndex:0];
}
这是两种方法都要注意唯一标识不要搞错了。