在另一个表视图中取消选中复选标记时添加重复项

时间:2015-05-26 13:34:42

标签: ios xcode uitableview

我在两个不同的视图中有2个tableview,我在第一个中使用地址簿获取联系人,工作正常现在我有联系人检查标记的更新,只显示另一个第二个表视图中的检查联系人以及从第一个联系人表视图取消选中然后它应该从第二个表视图中删除,但是当我从第一个表视图取消选中时,它会从下面的代码中添加该单元格值并删除第一个Index值。但是这里我只想从第二个表视图控制器中删除取消选中单元格值。

先谢谢。

第一个表格视图代码

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"CellID";
        UITableViewCell *aCell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (aCell == nil) {
            aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            aCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            aCell.selectionStyle = UITableViewCellSelectionStyleBlue;
        }

         aCell.textLabel.text = [self.tableData objectAtIndex:indexPath.section] ;





        if([tableData containsObject:indexPath])
        { aCell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else
        { aCell.accessoryType = UITableViewCellAccessoryNone; }


        return aCell;
    }



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

  //  NSString *stringg = [tableData objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    NSString *stringg = [tableData objectAtIndex:indexPath.section];
       // NSLog(@"Index path:- %@",indexPath);
    if(cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
               }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [_cat removeObjectAtIndex:indexPath.row];
    }
    [_cat addObject:stringg];

    NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:_cat];

    NSLog(@"data save %@",dataSave);
    [[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:@"stringname"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

来自其他视图的第二个表格视图代码

- (void)viewDidLoad {
    [super viewDidLoad];

    catagory = [[NSMutableArray alloc] init];


    //category

    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"stringname"];
    catagory = [NSKeyedUnarchiver unarchiveObjectWithData:data];

    _tabelViewInView.delegate = self;
    _tabelViewInView.dataSource = self;
    [_tabelViewInView reloadData];
    // Do any additional setup after loading the view from its nib.
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier1];

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


    cell.textLabel.text = [catagory objectAtIndex:indexPath.row];


    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *stringname1 = [catagory objectAtIndex:indexPath.section] ;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:stringname1 forKey:@"stringname1"];
    [defaults synchronize];

        ContactViewController *contact = [[ContactViewController alloc]init];
        [self presentViewController:contact animated:YES completion:nil];

}

0 个答案:

没有答案