Vineet Singh
Vineet Singh

Reputation: 4019

How to save data of tableview cell

i am using custom checkbox buttons in my table view and want to save the selected cell's data to a mutable array.... can anyone help me... Thanks

Upvotes: 1

Views: 1593

Answers (5)

janusfidel
janusfidel

Reputation: 8106

create a mutable array to store your selected data, lets call it 'yourSeparatedData' , set the tag of your checkbox in cellForRowAtIndexPath and set onCheck: method as target. the code will look like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"setMe";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyle………
    }
    checkBox = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    checkBox.frame = CGRectMake(customizeMe);
    if(yourSeparatedData && [yourSeparatedData indexOfObject:[yourTableViewDataSource objectAtIndex:indexPath.row]] != NSNotFound)
    {
        [checkBox setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
    }
       else {
        [checkBox setBackgroundImage:[UIImage imageNamed:@"unCheck.png"] forState:UIControlStateNormal];
        }
        [checkBox addTarget:self action:@selector(onCheck:) forControlEvents:UIControlEventTouchUpInside];
        [checkBox setTag:indexPath.row];

        [cell addSubview:checkBox];
        return cell;

}

-(void)onCheck:(id)sender {
     if(yourSeparatedData && [yourSeparatedData indexOfObject:[yourTableViewDataSource objectAtIndex:[sender tag]]] != NSNotFound)
       {
        [sender setBackgroundImage:[UIImage imageNamed:@"unCheck.png"] forState:UIControlStateNormal];
        [yourSeparatedData removeObject:[yourTableViewDataSource objectAtIndex:[sender tag]]];
       }
      else {
        [sender setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
        [yourSeparatedData addObject:[yourTableViewDataSource objectAtIndex:[sender tag]]];
      }
      [yourTableView reloadData];
 } 

this code is not tested, you are using checkbox so I assumed that you want to separate not just one data, at the end of the selection, you will have 'yourSeparatedData' with the objects picked from your tableView.

Upvotes: 3

Alok Srivastava
Alok Srivastava

Reputation: 197

you can try custom action in UITableView cell for button prees,you can also use put a check box image and on click you can change the image as checked here is the code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"identifire"];
 cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifire"] autorelease];
    cell.detailTextLabel.text=[id_Array objectAtIndex:indexPath.row];
    cell.detailTextLabel.hidden=YES;
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"n_cross" ofType:@"png"];
    UIImage *buttonImage1 = [[UIImage alloc] initWithContentsOfFile:path1];
    [button setImage:buttonImage1 forState:UIControlStateNormal];
    [button addTarget:self 
               action:@selector(customActionPressed:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Custom Action" forState:UIControlStateNormal];
    button.frame = CGRectMake(245.0f, 20.0f, 40.0f, 40.0f);
    [cell addSubview:button];
    [buttonImage1 release];
        CGRect imageFrame=CGRectMake(10,8,50,50);
        self.cellimage=[[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
        self.cellimage.image=[imageIdArray objectAtIndex:indexPath.row];
        [cell.contentView addSubview:self.cellimage];
        return cell;
}
-(void)customActionPressed :(id)sender
{
//Get the superview from this button which will be our cell
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
NSIndexPath *cell = [_tableView indexPathForCell:owningCell];
NSString *uid=[id_Array objectAtIndex:cell.row];
[id_Array removeObjectAtIndex:cell.row];
[_tableView reloadData];
[self performSelectorInBackground:@selector(ignoreRequest:) withObject:uid];
}

here i have an id_Array and on selection of a cell i just remove the object at that index

Upvotes: 1

Melbourne
Melbourne

Reputation: 541

Try this

- (void)onButtonClick {

    int numberOfSections = [tableView numberOfSections];

    for (int section = 0; section < numberOfSections; section++) {

        int numberOfRows = [tableView numberOfRowsInSection:section];

        for (int row = 0; row < numberOfRows; row++) {

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

            if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {

                // Cell is selected
               //here you can add that values into an array
            } else {

                // Cell is not selected
            }
        }
    }
}

Upvotes: 0

Nuzhat Zari
Nuzhat Zari

Reputation: 3408

When user selects any cell then in didSelectRowAtIndexPath you can add selected object dynamically.

[someMutableArr addObject:[tableArr objectAtIndex:indexPath.row]];

Upvotes: 0

Joshua Smith
Joshua Smith

Reputation: 6621

You have to do this manually, there is no facility in a UITableView or UITableViewController t o do this automatically.

Upvotes: 0

Related Questions