Ios UITableView单元格在高度更改后显示错误的内容
我正在用三个部分实现UITableView。第二部分的最后一行应该显示UIPicker的实例 因此,我更改了此特定行的高度,如下所示:Ios UITableView单元格在高度更改后显示错误的内容,ios,objective-c,uitableview,nsindexpath,heightforrowatindexpath,Ios,Objective C,Uitableview,Nsindexpath,Heightforrowatindexpath,我正在用三个部分实现UITableView。第二部分的最后一行应该显示UIPicker的实例 因此,我更改了此特定行的高度,如下所示: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat rowHeight = self.tableView.rowHeight; if (indexPath.section == RedZon
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat rowHeight = self.tableView.rowHeight;
if (indexPath.section == RedZoneSection && indexPath.row == MonitorConfigRow){
rowHeight = 162;
return rowHeight;
}
return rowHeight;
}
但是,当我添加该代码时,第三部分中的第一行(“仅来自”的警报)将UISwitch添加到它的视图中,该视图不应该存在,如下所示:
下面是我为第三部分实现的cellForRowAtIndexPath
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForTimeOfDayRestrictionsRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SettingsRowCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
switch (indexPath.row) {
case HourTimeZoneRow:
cell.textLabel.text = NSLocalizedString(@"Only Alert From", @"Only Alert Row");
break;
default:
break;
}
return cell;
}
编辑在错误位置再次显示的特定UI开关最初位于我的表格第一部分的第二个单元格中。下面是该部分的cellforrowatinexpath
实现:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForSubscribedNotificationsRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SettingsRowCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
UISwitch *cellSwitch = nil;
switch (indexPath.row) {
case RowOne:
cell.textLabel.text = NSLocalizedString(@"Row One", @"Row One");
cellSwitch = [[UISwitch alloc] init];
cell.accessoryView = cellSwitch;
break;
case RowTwo:
cell.textLabel.text = NSLocalizedString(@"Row Two", @"Row Two");
cell.accessoryView = cellSwitch;
break;
// cell.textLabel.text = nil ;
cell.accessoryView = UITableViewCellAccessoryNone;
accessoryViewIsShowing = FALSE;
break;
}
if (cell.accessoryView == nil) {
NSLog(@"Cell accessoryView is nil");
}
else if (cell.accessoryView != nil){
NSLog(@"Cell accessoryView is not nil");
}
NSLog(@"Section: %ld, Row: %ld", (long)indexPath.section, (long)indexPath.row);
return cell;
}
有人知道为什么使用更改特定单元格的高度会导致在另一个分区的单元格中显示不正确的内容吗?问题是因为在
表视图:CellForSubscribedNotificationsRowatineXpath:
方法中回收单元格时出现错误。更改其中一个单元格的高度会使此错误可见
您需要将代码添加到默认
案例中,以删除为行一
和行二
添加的附件和单元格文本:
default:
cell.textLabel.text = nil;
cell.accessoryView = nil;
break;
否则,除行一
和行二
以外的行中的“回收”单元格(以前是行一
或行二
)将包含回收单元格之前的旧文本和附件视图
解决此问题的另一种方法是。我找不到以编程方式将
UISwitch
添加到表格单元格的部分。您的单元格FortimeOfDayRestrictionsRowatingIndexPath:
中是否有其他代码没有显示以缩短示例?您好@DasLinkedLight,是的,我担心问题太长。我刚刚编辑了它,将“UISwitch”添加到第一行的单元格中。非常感谢。您正在回收一个单元格,但未能从旧单元格中删除开关。@HotLicks,我应该在哪里从旧单元格中删除开关?实现UITableViewCell的子类并使用prepareForReuse
,或者将“cleanup”逻辑放入cellForRow…
,如果单元格被回收,该逻辑将生效。这种推理是有意义的。不过,我确实试过你的建议;我仍然得到完全相同的行为:/@narner这很奇怪-在方法的逻辑中还有其他东西保留了一个旧的附件视图。在返回单元格之前,我会添加一个对NSLog
的调用,以查看两件事:行号和一个true/false,它指示单元格的附件视图是否为nil
。我尝试了一下,得到了以下输出:节:0,行:0,附件视图正在显示,节:0,行:1,附件视图未显示,节:0,行:0,附件视图未显示,节:0,行:1,附件视图未显示showing@narner真奇怪。我还将尝试设置cell.accessoryType=uitableviewCellAccessoryne代码>但我不确定它是否会改变什么:如果调试代码说附件视图是nil
,但它却出现了,它看起来像一个Cocoa touch bug。@纳纳这是一个好的开始-现在你知道代码逻辑中的某些东西与你认为它应该做的事情不同。您可以追溯您的逻辑以发现错误的代码路径,或者在设置单元格背景的同一位置采用“鸟枪式方法”并nil
退出附件视图。