不同的UITableView部分如何在不同的NSArray中存储值

时间:2014-12-29 08:07:04

标签: ios objective-c uitableview nsarray

我正在从Web服务中检索“样式”和“类别”的值。它们都在UITableView中单独显示。
还提供多种细胞选择选项。我已经做到了NSMutableArray,我希望当用户从样式部分中选择一个值时,它的值会进入一个数组,而其他部分值则会在其他数组中进行。当他取消选择值时,应将其从数组中删除。以下代码未显示存储与否的任何值。所有打印的都是(null)

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        if (indexPath.section==0)
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
            selected[row] = NO;
            NSLog(@"indexpath zero of un-select");
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
            selected[row] = NO;
            NSLog(@"indexpath else of un-select");
        }
    }
    else
    {
        if (indexPath.section==0)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            selected[row] = YES;
            [self.styleSelect objectAtIndex:indexPath.row];  
            NSLog(@"indexpath zero of select, %@",self.styleSelect[indexPath.row]); //this prints (null)

        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            selected[row] = YES;
            [self.categorySelect objectAtIndex:indexPath.row];
            NSLog(@"indexpath else of select, %@",self.categorySelect[indexPath.row]); //this prints (null)
        }
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

2 个答案:

答案 0 :(得分:0)

如果它打印" null"它只是意味着你没有初始化数组,或者你在这些索引上有NSNull对象。 NSLog之前的呼叫,即。访问者调用是多余的。即应该有addObject而不是objectAtIndex,还应该创建数组。

        if (!self.styleSelect) 
             self.styleSelect = [NSMutableArray array];
        [self.styleSelect addObject:@(indexPath.row)];   

        NSLog(@"indexpath zero of select, %@",self.styleSelect[indexPath.row]);

答案 1 :(得分:0)

试试这个

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        if (indexPath.section==0)
        {
            [styleArray removeObject:indexPath];
        }
        else
        {
            [categoryArray removeObject:indexPath];
        }
    }
    else
    {
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
        if (indexPath.section==0)
        {
            [styleArray addObject:indexPath];
        }
        else
        {
            [categoryArray addObject:indexPath];
        }
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
相关问题