eri*_*ell 5 uitableview nsindexpath ios
这是我的cellForRowAtIndexPath UITableView委托方法的删节代码:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"];
if (cell == nil) {
// No cell to reuse => create a new one
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"blahblahblah"] autorelease];
}
cell.textLabel.text = @"";
cell.detailTextLabel.text = @"";
cell.backgroundView = NULL; //problem here
// Initialize cell
//blah blah blah
//now to the good part...
if(indexPath.section == 1) {
cell.backgroundView = deleteButton;
cell.userInteractionEnabled = YES;
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
}
else if(indexPath.section == 0) {
NSLog(@"section: %i row: %i", indexPath.section, indexPath.row);
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"foobar";
//more stuff
break;
//lots more cases
default:
break;
}
}
return cell;
Run Code Online (Sandbox Code Playgroud)
}
我的问题是第1部分中的第一个单元格(第0部分有10个单元格,第1部分只有1个单元格)被分配了仅应该分配给第一部分的单元格0的信息.因此,它不是获取deleteButton背景等,而是获得标签标题"foobar".我不确定为什么会发生这种情况,因为我的if语句很清楚.有任何想法吗?
编辑:将backgroundView设置为NULL会导致带有文本的单元格在离开视图时返回时没有任何背景.所以这不是一个可行的解决方案.此外,detailTextLabel的文本仍设置在不应包含任何文本的单元格上.
这是它的外观,单元格backgroundViews设置为nil,文本显示在删除单元格中不应该:

解决方案,由Alex Deem推荐(用此代码替换旧的dequeue代码):
NSString* identifier;
if(indexPath.section == 0)
identifier = @"0";
else
identifier = @"1";
UISwitch *switchView;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
// No cell to reuse => create a new one
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10635 次 |
| 最近记录: |