gpt4 book ai didi

iOS UITableView 正在绘制奇怪的单元格

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:01 24 4
gpt4 key购买 nike

我有一个 UITableView,它应该只在第一部分第一行显示一个 UISwitch,但由于某种原因,开关显示在第四部分。此外,每当打开/关闭开关时,一个新的 UISwitch 就会出现在另一个单元格中。它的行为非常奇怪。任何帮助将不胜感激。

代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

PrettyTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.tableViewBackgroundColor = tableView.backgroundColor;
}

[cell prepareForTableView:tableView indexPath:indexPath];
cell.cornerRadius = 10;

if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Push Notifications";
pushNotificationSwitch = [[UISwitch alloc] init];
pushNotificationSwitch.on = pushSwitchState;
cell.accessoryView = pushNotificationSwitch;
[pushNotificationSwitch addTarget:self action:@selector(pushSwitch:) forControlEvents:UIControlEventValueChanged];
cell.userInteractionEnabled = YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}

if (indexPath.section == 1) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Department Based";
if (departmentBased) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.userInteractionEnabled = YES;
}
if (indexPath.row == 1) {
cell.textLabel.text = @"Location Based (GPS)";
if (locationBased) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
} cell.userInteractionEnabled = YES;
}
}

if (departmentBased) {
if (indexPath.section == 2) {
if (indexPath.row == 0) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 1) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 2) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 3) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 4) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 5) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 6) {
cell.textLabel.text = @"xxxxxx";
}
}
if (indexPath.section == 3) {
if (indexPath.row == 0) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 1) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 2) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 3) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 4) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 5) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 6) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 7) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 8) {
cell.textLabel.text = @"xxxxxx";
}
}
}
return cell;
}

最佳答案

您正在体验 iOS 中单元可重用性的影响。您对所有单元格使用相同的单元格标识符,因此当您滚动时,旧单元格将变得可用并从 tableView 中出列。

尝试在之后立即将 cell.accessoryView 设置为 nil:

if (cell == nil) {
cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.tableViewBackgroundColor = tableView.backgroundColor;
}

cell.accessoryView = nil;

或者,为第一个 indexPath 选择一个不同的单元格标识符,确保在出队时考虑到这一点。

BOOL firstCell = indexPath.row == 0 && indexPath.section == 0;
NSString *CellIdentifier = firstCell ? @"CellWithSwitch" : @"Cell";

PrettyTableViewCell *cell =
[tableViewdequeueReusableCellWithIdentifier: CellIdentifier];

if (cell == nil) {
cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.tableViewBackgroundColor = tableView.backgroundColor;
}

关于iOS UITableView 正在绘制奇怪的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14918309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com