Я настроил UITableView с 3 разделами, которые извлекаются из 3 NSArrays, содержащих ключи для каждого раздела.Я попытался настроить оператор switch, чтобы вручную установить detailTextLabels и вспомогательные представления для каждой ячейки, но каждый раз, когда я прокручиваю представление таблицы, ячейки, по-видимому, изменяются сами по себе.Есть ли более эффективный способ сделать это?
Это один метод, который я пытался использовать:
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