Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c DetailTextLabel在UITableViewCell中不断更改_Objective C_Ios_Uitableview - Fatal编程技术网

Objective c DetailTextLabel在UITableViewCell中不断更改

Objective c DetailTextLabel在UITableViewCell中不断更改,objective-c,ios,uitableview,Objective C,Ios,Uitableview,我已经设置了一个UITableView,其中包含3个部分,从包含每个部分的键的3个NSArray中提取。我尝试设置一个switch语句来手动设置每个单元格的detailTextLabels和附件视图,但每次我滚动表格视图时,单元格似乎都会自行改变。有没有更有效的方法 这是我尝试使用的一种方法: switch (indexPath.section) { // info case 0:{ cell.textLabel.text = [infoKeysArray ob

我已经设置了一个UITableView,其中包含3个部分,从包含每个部分的键的3个NSArray中提取。我尝试设置一个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];
    }
}

这两次尝试都在
cellforrowatinexpath

我最终自己解决了这个问题,这完全要感谢@Till的评论。
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]
的工作方式似乎是重用现有的单元格,因此为了在表中使用多个单元格类型,必须使用不同的重用标识符。或者,由于我只有7个或8个表格视图单元格,我只需将所有不包含UISwitch的单元格的单元格附件视图设置为nil,如下所示:

[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;
}

这种方式省去了我处理多个重用标识符的麻烦。

你真的应该引用你最相关的代码。结果是,我滚动的越多,一些单元格的附件视图和其他单元格的详细文本标签开始出现在每一行中,即使它开始正常。似乎[indexPath section]并不代表整个部分,而是显示的部分,至少这是唯一对我有意义的事情。您是否知道您的单元格被重用,因此以前的有效设置仍处于活动状态?在初始化单元格时,您必须完全重置任何应该显示或隐藏的内容。但这是否可以解释为什么最初只有我想要的单元格在两个不同的位置具有辅助视图,例如UISwitch,但在上下滚动几次后,每个单元格中都有一个UISwitch?那么我的代码会在滚动几下后使UISwitches出现在每个单元格中吗?将该开关添加到单元格中的部分。如果以前在第0节中使用过,则重用的单元格将包含该开关(无论现在在哪个节中使用)。对看起来完全不同的单元格使用不同的单元格标识符。