Question

How can I set first row of tableview checkmarked when the app started? I mean when I start app now, there are few rows in tableview and when I click on some of them its accessory change to checkmark. When click another one, the another one comes checkmarked and previous checkmark dissapears. So now I want first row of tableview to be checkmarked when app is started and then when I click another row it 'uncheckmark' and 'checkmark' new row etc...

Was it helpful?

Solution

Try the options suggested in these posts how to apply check mark on table view in iphone using objective c? or iPhone :UITableView CellAccessory Checkmark

Basically you need to keep track of the checkmarks using say a dictionary or so. And In viewDidLoad or init method make the first cell as checked in the dictionary. While drawing cells, always check if the corresponding entry in the dictionary is checked or not and display check mark accordingly. When user taps on a cell, modify the value in dictionary to checked/unchecked.

Update: Here is a sample code.

In .h file declare a property as

@property(nonatomic) NSInteger selectedRow;

Then use the below code,

- (void)viewDidLoad {
  //some code...

  self.selectedRow = 0; //if nothing should be selected for the first time, then make it as -1
  //create table and other things
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // create cell and other things here

    if (indexPath.row == self.selectedRow)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // some other code..

    if (indexPath.row != self.selectedRow) {
       self.selectedRow = indexPath.row;
    }

    [tableView reloadData];
}

OTHER TIPS

You could set an integer to a variable similar to "checkedCell", have that value default to cell 0, and in the cellForRowAtIndexPath check to see if the cell is already checked, if not change the accessory to make it checked and update your variable.

-(void)viewWillAppear{
     currentCheckedCell = 0;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //
    // Create cells -- if indexpath.row is equal to current checked cell reload the table with the correct acessories.
    //
    static NSString *cellIdentifier = @"RootMenuItemCell";
    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {

        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
        for (UIView *view in nibContents) {
            if ([view isMemberOfClass:[MyCell class]]) {
                cell = (MyCell *)view;
                break;
            }
        }

        //OTHER CELL CREATION STUFF HERE

        // cell accessory
        UIImage *accessory = [UIImage imageNamed:@"menu-cell-accessory-default.png"];
        UIImage *highlighted = [UIImage imageNamed:@"menu-cell-accessory-selected.png"];
        if(indexPath.row == currentCheckedCell){
            //DEFAULT ACCESSORY CHECK 
            // cell.accessoryType = UITableViewCellAccessoryCheckmark;

            UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessory highlightedImage:highlighted];
        }else{
             //DEFAULT ACCESSORY NONE 
            // cell.accessoryType = UITableViewCellAccessoryNone;
              UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessory highlightedImage:accessory];
         }
        [cell setAccessoryView:accessoryView];

    } 
return cell;
}

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
      //UPDATE SELECTED CELL & RELOAD TABLE
      currentCheckedCell = indexPath.row;
      [self.tableview reloadData];
}

Also worth noting that my examples uses custom images for accessories. If you check out the link @ACB provided you'll find a very similar concept.

You can use,

if (firstCellSelected)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } 

Set the firstCellSelected flag in viewDidLoad method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top