NSIndexPath仅检索第一个单元格值

时间:2014-07-08 23:09:18

标签: objective-c nsindexpath

我尝试打印出用户在某些单元格中更新UISwitch的值时调用的方法中单击的值的值。有四个单元格将UISwitch设置为单元格的附件。我已经有基础设施来检查调用[self.basicObjects objectAtIndex:indexPath.row]的值,但问题是,无论我按哪个单元格,0包含的NSMutableArray值调用四个单元格的标题。

- (void)switchChanged:(id)sender cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    NSLog(@"self.basicObjects value: %@", [self.basicObjects objectAtIndex:indexPath.row]);
}

作为旁注,正如我在查看类似问题时,同样重要的是要注意self.settingsTable是一个分组UITableView,这四个单元格位于第1部分(0,1,2) )。其他两个部分不包含任何UISwitch作为附件类型,它只是此部分,只有这一部分引用了switchChanged:cellForRowAtIndexPath:方法。

编辑 - 下午7:31

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"TableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17.5];

    if (indexPath.section == 0)
    {
        // change the text to standard case
        // change text color
    }

    else if (indexPath.section == 1)
    {
        cell.textLabel.text = [self.basicObjects objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        self.switchProperty = [[UISwitch alloc] initWithFrame:CGRectZero];
        cell.accessoryView = self.switchProperty;

        if ([[self.basicStatus objectAtIndex:indexPath.row] isEqual:@"YES"])
        {
            [self.switchProperty setOn:YES animated:YES];
            [self.switchProperty addTarget:self action:@selector(switchChanged:cellForRowAtIndexPath:) forControlEvents:UIControlEventValueChanged];
        }

        else if ([[self.basicStatus objectAtIndex:indexPath.row] isEqual:@"NO"])
        {
            [self.switchProperty setOn:NO animated:YES];
            [self.switchProperty addTarget:self action:@selector(switchChanged:cellForRowAtIndexPath:) forControlEvents:UIControlEventValueChanged];
        }
    }
    else if (indexPath.section == 2)
    {
        cell.textLabel.text = [self.objects objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [self.objectsType objectAtIndex:indexPath.row];
        cell.detailTextLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17.5];
        if ([[self.objectsStatus objectAtIndex:indexPath.row] isEqual:@"Setup"])
            cell.detailTextLabel.textColor = [UIColor colorWithRed:0.298 green:0.851 blue:0.392 alpha:1]; // #4CD964 (green)
        else if ([[self.objectsStatus objectAtIndex:indexPath.row] isEqual:@"Not Setup"])
            cell.detailTextLabel.textColor = [UIColor colorWithRed:0.898 green:0.267 blue:0.267 alpha:1]; // #E54444 (red)
    }

    return cell;
}

2 个答案:

答案 0 :(得分:0)

您不能以这种方式在action方法中传递indexPath。您只能传递发件人,也可以选择传递触发操作的事件。您需要以不同的方式获取indexPath。一种常见的方法是为交换机提供一个等于indexPath.row的标记,并使用该标记作为数组的索引。

答案 1 :(得分:0)

这是一个问题。你不能像这样发送第二个参数。

[self.switchProperty addTarget:self action:@selector(switchChanged:cellForRowAtIndexPath:) forControlEvents:UIControlEventValueChanged];

我建议你设置一个标签,并按如下所示使用它。

[self.switchProperty setTag:indexPath.row];
[self.switchProperty addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

并在switchChanged:将...

- (void)switchChanged:(id)sender
{
    NSLog(@"self.basicObjects value: %@", [self.basicObjects objectAtIndex:[sender tag]]);
}