我已经设置了一个包含 3 个部分的 UITableView,它从 3 个包含每个部分的键的 NSArrays 中提取。我尝试设置一个 switch 语句来手动设置每个单元格的 detailTextLabels 和附件 View ,但每次滚动表格 View 时,单元格似乎都会自行更改。有更有效的方法吗?
这是我尝试使用的一种方法:
switch (indexPath.section)
{
// info
case 0:{
cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:@"Duration"])
{
cell.detailTextLabel.text = [task stringForDuration];
}
if ([cell.textLabel.text isEqualToString:@"Elapsed"])
{
// elapsed time
}
if ([cell.textLabel.text isEqualToString:@"Today"])
{
UISwitch *todaySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[todaySwitch addTarget:self action:@selector(todaySwitchValueChanged:) forControlEvents:UIControlEventValueChanged];
[todaySwitch setOn:task.isToday];
cell.accessoryView = todaySwitch;
[todaySwitch release];
}
}
break;
// actions
case 1:{
cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:@"Priority"])
{
// priority
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
if ([cell.textLabel.text isEqualToString:@"Finish Early"])
{
// finish early
}
if ([cell.textLabel.text isEqualToString:@"Set New Time"])
{
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
if ([cell.textLabel.text isEqualToString:@"Repeating"])
{
UISwitch *repeatingSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[repeatingSwitch addTarget:self action:@selector(repeatingSwitchValueChanged:) forControlEvents:UIControlEventValueChanged];
[repeatingSwitch setOn:task.isRepeating];
cell.accessoryView = repeatingSwitch;
[repeatingSwitch release];
//cell.detailTextLabel.text = nil;
}
}
break;
// details
case 2:{
cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:@"Specifics"])
{
cell.detailTextLabel.text = task.specifics;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
if ([cell.textLabel.text isEqualToString:@"Repeated"])
{
// times repeated
}
}
break;
default:
break;
}
这是另一个:
// Info section
if ([indexPath section] == 0)
{
// duration
cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row];
if (indexPath.row == 0)
{
cell.detailTextLabel.text = [task stringForDuration];
}
// elapsed time
if (indexPath.row == 1)
{
//cell.detailTextLabel.text = [task stringForTotalElapsedTime];
}
// today status
if (indexPath.row == 2)
{
UISwitch *todaySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[todaySwitch addTarget:self action:@selector(todaySwitchValueChanged:) forControlEvents:UIControlEventValueChanged];
[todaySwitch setOn:task.isToday];
cell.accessoryView = todaySwitch;
[todaySwitch release];
}
}
// actions section
if ([indexPath section] == 1)
{
cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row];
// priority
if (indexPath.row == 0)
{
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
// finish early
if (indexPath.row == 1)
{
//[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
// set new time
if (indexPath.row == 2)
{
//cell.accessoryView = repeatingSwitch;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
// repeating
if (indexPath.row == 3)
{
UISwitch *repeatingSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[repeatingSwitch addTarget:self action:@selector(repeatingSwitchValueChanged:) forControlEvents:UIControlEventValueChanged];
[repeatingSwitch setOn:task.isRepeating];
cell.accessoryView = repeatingSwitch;
[repeatingSwitch release];
cell.detailTextLabel.text = nil;
}
}
// details section
if ([indexPath section] == 2)
{
cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row];
// specifics
if (indexPath.row == 0)
{
//cell.detailTextLabel.text = taskDetails.specifics;
cell.detailTextLabel.text = task.specifics;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
// repeated
if (indexPath.row == 1)
{
[cell setAccessoryView:nil];
}
}
这两个尝试都在 cellForRowAtIndexPath
中
最佳答案
我最终自己解决了这个问题,完全感谢@Till 的评论。 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
的工作方式似乎是重用现有的单元格,因此为了在表格中使用多种单元格类型,您必须使用不同的重用标识符。或者,因为我只有 7 或 8 个表格 View 单元格,所以我只是将单元格附件 View 设置为 nil,以便我不想包含 UISwitch 的所有单元格,如下所示:
[cell setAccessoryType:UITableViewCellAccessoryNone];
switch ([indexPath section])
{
case 0:
{
cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row];
switch (indexPath.row) {
case 0:
{
cell.detailTextLabel.text = task.stringForDuration;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell.accessoryView = nil;
}
break;
case 1:
{
cell.detailTextLabel.text = @"00:00";
[cell setAccessoryType:UITableViewCellAccessoryNone];
cell.accessoryView = nil;
}
break;
case 2:
{
UISwitch *todaySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[todaySwitch addTarget:self action:@selector(todaySwitchValueChanged:) forControlEvents:UIControlEventValueChanged];
[todaySwitch setOn:task.isToday];
cell.accessoryView = todaySwitch;
[todaySwitch release];
[cell setAccessoryType:UITableViewCellAccessoryNone];
cell.detailTextLabel.text = @"";
}
break;
default:
break;
}
}
break;
case 1:
{
cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row];
switch (indexPath.row) {
case 0:
{
cell.detailTextLabel.text = @"Low";
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell.accessoryView = nil;
}
break;
case 1:
{
cell.detailTextLabel.text = @"";
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell.accessoryView = nil;
}
break;
case 2:
{
UISwitch *repeatingSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[repeatingSwitch addTarget:self action:@selector(repeatingSwitchValueChanged:) forControlEvents:UIControlEventValueChanged];
[repeatingSwitch setOn:task.isRepeating];
cell.accessoryView = repeatingSwitch;
[repeatingSwitch release];
cell.detailTextLabel.text = @"";
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
break;
default:
break;
}
}
break;
case 2:
{
cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row];
switch (indexPath.row) {
case 0:
{
cell.detailTextLabel.text = task.specifics;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell.accessoryView = nil;
}
break;
case 1:
{
cell.detailTextLabel.text = @"0";
[cell setAccessoryType:UITableViewCellAccessoryNone];
cell.accessoryView = nil;
}
break;
default:
break;
}
break;
}
default:
break;
}
这种方式省去了处理多个重用标识符的麻烦。
关于objective-c - DetailTextLabel 在 UITableViewCell 中不断变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9050373/