You can add and subtract 1 from my cell.textLabel.text. I am adding 1 with this method:
- (IBAction)addLabelText:(id)sender{
num = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +1];
number = [[NSMutableArray alloc]initWithObjects:num, nil];
[myTableView reloadData];
}
And i cant get the textLabel to subtract! here is my method:
- (IBAction)subtractLabelText:(id)sender
{
if ( [[cell.textLabel text] intValue] == 0){
num = [NSString stringWithFormat:@"%d",[num intValue] +0];
[number addObject:num];
}
else{
num = [NSString stringWithFormat:@"%d",[num intValue] -1];
[number addObject:num];
}
}
In my cellForRowAtIndexPath method, i am trying to set the label’s text with this line:
cell.textLabel.text = [number objectAtIndex:indexPath.row];
The + button works, but the – button doesnt. How could i fix this? Thanks in advance!
CELLFORROW
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
addBtn = [[UIButton alloc]init];
addBtn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[addBtn setFrame:CGRectMake(220,10,25,55)];
[addBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside];
[addBtn setTitle:@"+" forState:UIControlStateNormal];
[addBtn setEnabled:YES];
[cell addSubview:addBtn];
subBtn = [[UIButton alloc]init];
subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[subBtn setFrame:CGRectMake(260,10,25,55)];
[subBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside];
[subBtn setTitle:@"-" forState:UIControlStateNormal];
[subBtn setEnabled:YES];
[cell addSubview:subBtn];
//cell.textLabel.text = @"1";
}
//cellText.hidden=!self.editing;
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.imageView.image = [imageArray objectAtIndex:indexPath.row];
cell.textLabel.text = [number objectAtIndex:indexPath.row];
return cell;
}
It will crash because the cell is not the same cell on which your + and – buttons are there.
You have to get the “UITableViewCell” in which you clicked the “+” or “-” button. And then manipulate the text as you required.