Hello I have a tableView which is populating from an array(tableData). there are buttons on each cell. I wanted to know how to get particular data of cell into an array when i select that cell's button and similarly how i remove it when i again select that button on cell from array? this is what iam doing till now.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
// Configure the cell.
if (tableView.tag == 1) {
cell.textLabel.text = [unitArry objectAtIndex:indexPath.row];
}
else {
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"g1.png"]];
[cell setBackgroundView:img];
[img release];
cell.productLbl.text= [NSString stringWithFormat:@"%@",[[tableData objectAtIndex:indexPath.row]objectForKey:@"nameOfVegetables"] ];
cell.bPriceLbl.text = [NSString stringWithFormat:@"Rs %d/Kg",
[[[tableData objectAtIndex:indexPath.row] objectForKey:@"priceOfVegetables"] intValue]];
cell.qtyLbl.text = [NSString stringWithFormat:@"QTY: %@ %@",[[tableData objectAtIndex:indexPath.row]
objectForKey:@"quantityOfVegetables"],[[tableData objectAtIndex:indexPath.row] objectForKey:@"unitOfVegetables"]] ;
cell.tPriceLbl.text = [NSString stringWithFormat:@"TOTAL: %@",[[tableData objectAtIndex:indexPath.row]
objectForKey:@"totalPriceOfVegetables"]];
BOOL selected;
if(selected){
cell.unchecked.tag=indexPath.row;
[cell.unchecked addTarget:self action:@selector(buttonMethod:) forControlEvents:UIControlEventTouchUpInside];
}
//else {
//
// cell.unchecked.tag=indexPath.row;
// [cell.unchecked addTarget:self action:@selector(buttonRemoveMethod:) forControlEvents:UIControlEventTouchUpInside];
// }
}
return cell;
}
-(void)buttonMethod:(id)sender{
UIButton *button = (UIButton *)sender;
row = button.tag;
NSNumber *yWrapped = [NSNumber numberWithInt:row];
[btnArry addObject:yWrapped];
NSLog(@"addObjectInbtnArry:%@",btnArry);
}
-(void)buttonRemoveMethod:(id)sender{
//????
}
//here i wanted to add all cells index to an array and fetch the data from my tableData
- (void)buy:(id)sender
{
[tmpArry addObjectsFromArray:btnArry];
for (id obj in btnArry) {
[tableData objectAtIndex:[obj intValue]];
}
}
plz suggest me also how to remove the particular cell index from the same array when i click on the same button again of cell.