UITableView多项选择在使用XIB

时间:2017-07-08 11:04:13

标签: ios objective-c uitableview

我是iOS的新手,我在表视图上有多个选择时遇到问题。

当我使用pushview控制器选择值时取消选择
我正在使用这样的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
    UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text=[NSString stringWithFormat:@"%@",[reportshortActivityarray objectAtIndex:indexPath.row]];

    if([Selectedarray containsObject:[reportshortActivityarray objectAtIndex:indexPath.row]])
    {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
    else
    {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if(![Selectedarray containsObject:[reportshortActivityarray objectAtIndex:indexPath.row]]){
        [Selectedarray addObject:[reportshortActivityarray objectAtIndex:indexPath.row]];
        txtactivity.text = [Selectedarray componentsJoinedByString:@","];
        DefaultActivityString=txtactivity.text;
    }

    NSLog(@"Selected Value =%@",txtactivity.text);

    if(![SelectedIDarray containsObject:[reportidActivityarray objectAtIndex:indexPath.row]]){
        [SelectedIDarray addObject:[reportidActivityarray objectAtIndex:indexPath.row]];
        lblactivity.text = [SelectedIDarray componentsJoinedByString:@","];
    }

    NSLog(@"Selected Value =%@",lblactivity.text);



    [[activitytable cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
    activitytable.hidden=NO;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{


    if([Selectedarray containsObject:[reportshortActivityarray objectAtIndex:indexPath.row]]){
        [Selectedarray removeObject:[reportshortActivityarray objectAtIndex:indexPath.row]];
        txtactivity.text = [Selectedarray componentsJoinedByString:@","];
    }

    if([SelectedIDarray containsObject:[reportidActivityarray objectAtIndex:indexPath.row]]){
        [SelectedIDarray removeObject:[reportidActivityarray objectAtIndex:indexPath.row]];
        lblactivity.text = [SelectedIDarray componentsJoinedByString:@","];
    }
    NSLog(@"Selected Value =%@",lblactivity.text);

    [[activitytable cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
    activitytable.hidden=NO;
}

进行多项选择

按钮

-(IBAction)btnaddClick:(id)sender
{
 IncidentHSQE *inc=[[IncidentHSQE alloc] initWithNibName:@"IncidentHSQE" bundle:nil];
            [self.navigationController pushViewController:inc animated:YES];
}

删除所选数据。 如何恢复所选数据。 提前致谢!

0 个答案:

没有答案