Xavi Valero
Xavi Valero

Reputation: 2057

Store boolean value in UITableviewcell

How can I store a boolean value in each row of a UITableview. I need to retrieve the boolean value stored in the cell, when that particular cell is selected.

Upvotes: 0

Views: 1276

Answers (5)

HelmiB
HelmiB

Reputation: 12333

I recommend to use tag property in UITableViewCell.

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;            
// returns nil if cell is not visible or index path is out of range
{

    static NSString *identifier = @"MyIndetifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier] autorelease];
  }
  //make sure tu put here, or before return cell. 
  cell.tag = 0; //0 =NO, 1=YES;

  return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    BOOL boolean = cell.tag; // return 0 or 1. based on what boolean you set on this particular row.
}

Upvotes: 1

User
User

Reputation: 42

You need to implement method of UITableView

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;            
// returns nil if cell is not visible or index path is out of range
{
 //create cell here
static NSString *CellIdentifier = @"Cell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier] autorelease];
  }
  return cell;
}
  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //by using indexpath.row you can access the cell on which user clicked. }

Upvotes: 0

Nebary
Nebary

Reputation: 499

There are so many ways:
1. You can use UITableViewCell.tag property
2. You can create your own cell class inherited from UITableViewCell and add there normal property for you BOOL value
3. You can use array associated with your tableview and when you get cell selected, just use indexPath to find associated value in your array
etc.

Upvotes: 2

xda1001
xda1001

Reputation: 2459

NSMutableArray *tableData;

every table cell associate with a NSMutableDictionary store in tableData, you can set a NSNumber(store bool) to the Dictionary.

Upvotes: 0

nevan king
nevan king

Reputation: 113767

You probably have some other storage, where you keep things like the table cell title or subtitle. Store the boolean there. Use this to convert the bool to something you can put in an array or dictionary.

[NSNumber numberWithBool:YES]

For example, if you're using an NSArray of strings to store the titles, instead use an array of dictionaries. Each dictionary would have a "title" and (for example) "isActive" boolean (stored as an NSNumber).

Upvotes: 0

Related Questions