我有一个UITableView,它只能在第一行第一行显示UISwitch,但出于某种原因,开关显示在第4部分。此外,当开关打开/关闭时,新的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;
}
答案 0 :(得分:1)
您正在体验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;
}