开发者

Using accessory check mark in a table view

开发者 https://www.devze.com 2023-03-26 21:47 出处:网络
I have a array of 8 items put in a table view.I am using the following code to set the check mark on tap of a row

I have a array of 8 items put in a table view.I am using the following code to set the check mark on tap of a row

[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];

.But my problem is 1] whenever i tap my first row ,the fifth rows check mark also get showed up. And 2] when i scroll the table view after checking a row , and return back , checked mark gets disappears. How to get rid of these problem. thanks in advance

here is the code

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

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    // find the cell being touched and update its checked/unchecked image
   CellView *targetCustomCell = (CellView *)[tableView cellForRowAtIndexPath:indexPath];
   开发者_Python百科 [targetCustomCell checkAction:nil];

    if ([arrData count]>0) 
    {
        if ([arrData containsObject:[arrName objectAtIndex:indexPath.row]]) {
            [arrData removeObject:[arrName objectAtIndex:indexPath.row]];
            [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];   
            NSLog(@"data removed");
            NSLog(@"arrData%@",arrData);
        }
        else {
            [arrData addObject:[arrName objectAtIndex:indexPath.row]];
            [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];   
            NSLog(@"data added");
            NSLog(@"tableArray%@",arrData);
        }

    }
    else {
        [arrData addObject:[arrName objectAtIndex:indexPath.row]];
        [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];   
        NSLog(@"data added");
        NSLog(@"arrData%@",arrData);
    }
   } 

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

        CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
        if (cell == nil)
        {
        cell = (CellView *)[[[CellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
        }
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:13];  
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.textAlignment = UITextAlignmentLeft;
        cell.textLabel.text = [arrName objectAtIndex:indexPath.row];
        return cell;
    }


for your 1) problem you have provide some code

2) problem :

you need to remember your last selected row for that you need to store your last selected index number by :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    selectedIndex = indexPath.row;
    [tableView reloadData]; 
}

and in your

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   if (indexPath.row == selectedIndex) 
  {
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
  }
     cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}

hope it helps you.

0

精彩评论

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