Ios UITableViewCell重复使用时为空
我将UITableView与故事板中定义的各种单元格一起使用。这些样式都是“Subtitle”。对于一种单元格类型,标识符是“label”,它在我的表中的两个位置使用,并通过以下方式获取:Ios UITableViewCell重复使用时为空,ios,uitableview,Ios,Uitableview,我将UITableView与故事板中定义的各种单元格一起使用。这些样式都是“Subtitle”。对于一种单元格类型,标识符是“label”,它在我的表中的两个位置使用,并通过以下方式获取: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"label" forIndexPath:indexPath]; 填充文本标签的代码为: // cell {0,0} {section,row} cell.textLa
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"label" forIndexPath:indexPath];
填充文本标签的代码为:
// cell {0,0} {section,row}
cell.textLabel.text = @"";
cell.detailTextLabel.text = @"Subtitle 1";
NSLog(@"%@",cell); // <UITableViewCell: 0x13ee183c0; frame = (0 26; 320 52); text = ''; ...
要查看问题,请向下滚动至第6节(请参阅单元格中的“Version X”),然后滚动回顶部。顶行将为空(应显示“这是详细信息文本”)。下一步向下滚动,使第0行不可见,然后滚动回顶部(第0行将显示详细文本)
以下是我的故事板示例(选择了“标签”行):
请使用以下命令:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(@"new one");
}
希望这能有所帮助/解决方案:在
表格的末尾添加[cell layoutifneed]
查看:cellforrowatinexpath:
(当使用带有各种原型单元的故事板时,这可能是苹果的一个bug?)
通过粘贴CellForRowatineXpath的整个代码而不查看整个方法,很难说出哪里出了问题-但是从您在这里所做的内容来看,我想问,如果您希望在特定位置使用静态内容,是否有必要使用动态单元格?如果您有明确的内容,您可能需要考虑静态单元格。@ Shai:我已经添加了更多代码。@德里克:我的UITabLVIEW是一个设置页面。每个单元可以有一个开关、一个滑块、一个步进器等,每种类型都有一个(动态)原型。一种类型是“标签”(标识符),没有添加控件。在查找表中指定每个节和行的特定类型,以及有关该行的文本和其他详细信息。如果我想更改UITableView行,我只需更改查找表。当使用带有原型单元格的情节提要时,dequeueReusableCellWithIdentifier:forIndexPath:从不返回nil。是的,这是iOS9中修复的iOS8错误。看见
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *resueIdentifier;
if ( indexPath.section == 0 && indexPath.row == 0 ) {
resueIdentifier = @"label"; // in the nib, this is a cell with no other controls
} else if ( indexPath.section == 6 && indexPath.row == 0 ) {
resueIdentifier = @"label";
} else { // all other rows for demo contain a UISwitch
resueIdentifier = @"switch"; // in the nib, this is a cell with a UISwitch (e.g., Settings)
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resueIdentifier forIndexPath:indexPath];
// never nil -- that always returns a cell from the storyboard/nib
if ( indexPath.section == 0 && indexPath.row == 0 ) {
cell.textLabel.text = @""; // no title
cell.detailTextLabel.text = @"This is detail text";
} else if ( indexPath.section == 6 && indexPath.row == 0 ) {
cell.textLabel.text = @"Version X";
cell.detailTextLabel.text = @""; // no detail text (this causes the problem)
} else {
cell.textLabel.text = @"X";
cell.detailTextLabel.text = @"Y";
}
return cell;
}
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(@"new one");
}