开发者

Multiple taps on the same row in a table view

开发者 https://www.devze.com 2023-03-02 22:45 出处:网络
I am working on putting a checkbox in every row of a table view. When I tap a row, its value should get saved into an array, on tap of another row, its value should likewise get saved to the same arra

I am working on putting a checkbox in every row of a table view. When I tap a row, its value should get saved into an array, on tap of another row, its value should likewise get saved to the same array, and on a tap of the same row, the value should get deleted from the array.

Please suggest to me how to implement this one. I was using the following code in didSelectRowAtIndexPath method but I was not able to do it.

if([arrData count]==0)
{
    strlast = [arrName objectAtIndex:indexPath.row];开发者_StackOverflow
    [arrData addObject:strlast];
    NSLog(@"string checked in arrData   %@",strlast);
}
else 
{
    for(int i = 0 ;i < [arrData count]; i++) 
    {
        NSLog(@"[arrData count]:%d",[arrData count]);
        strSelected = [arrName objectAtIndex:indexPath.row];
        NSLog(@"strSelected:%@",strSelected);

        for(int i = 0 ;i < [arrData count]; i++) 
        {
            if([strSelected caseInsensitiveCompare:[arrData objectAtIndex:i]])
            {
                [arrData addObject:strSelected];
                NSLog(@"arrData:%@",arrData);
            }
        }
    }
}


list is the array name which contains all the data that is viewed in tableview replace it with your own array name Suppose tableArray is your array in which values are inserted and deleted. in .h file

NSMutableArray *tableArray;

in .m file in view didload

tableArray=[[NSMutableArray alloc]init];

tableview didselect row method:-

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




if ([tableArray count]>0) {

        if ([tableArray containsObject:[list objectAtIndex:indexPath.row]]) {
            [tableArray removeObject:[list objectAtIndex:indexPath.row]];
            NSLog(@"data removed");
            NSLog(@"tableArray%@",tableArray);
        }
        else {
            [tableArray addObject:[list objectAtIndex:indexPath.row]];
            NSLog(@"data added");
            NSLog(@"tableArray%@",tableArray);
        }

    }
    else {
        //[tableArray addObject:[NSString stringWithFormat:@"%d",indexPath.row]];
        [tableArray addObject:[list objectAtIndex:indexPath.row]];
        NSLog(@"data added");
        NSLog(@"tableArray%@",tableArray);
    }


}

release the array in dealloc I have tested the code I hope it might help you....

0

精彩评论

暂无评论...
验证码 换一张
取 消