use*_*179 4 objective-c uitableview ios xcode5
我需要使用表中关联的NIB加载自定义单元格而不使用"dequeueReusableCellWithIdentifier:"
单元格必须加载新的,不想重用旧的..
假设该类被称为"CustomCell.h和CustomCell.m"和NIB CustomCell.xib
如何分配和初始化单元格不使用它们?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = ??
//cell customization
return cell
Run Code Online (Sandbox Code Playgroud)
}
如何解决这个问题?
您可以使用loadNibNamedNSBundle的方法加载CustomCell,如下所示.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"NibFile" owner:self options:nil] objectAtIndex:0]
//cell customization
return cell
}
Run Code Online (Sandbox Code Playgroud)
为什么你不想重复使用这个细胞?任何具体原因?每次创建新单元格时,内存消耗会更多.如果需要,您可以使用以下代码重用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"CellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell)
cell = [[[NSBundle mainBundle] loadNibNamed:@"NibFile" owner:self options:nil] objectAtIndex:0]
//cell customization
return cell
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2546 次 |
| 最近记录: |