您可以从我的cell.textLabel.text
中添加和减去1
。我用这种方法加1:
- (IBAction)addLabelText:(id)sender{
num = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +1];
number = [[NSMutableArray alloc]initWithObjects:num, nil];
[myTableView reloadData];
}
我无法得到要减去的textLabel!这是我的方法:
- (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];
}
}
在我的cellForRowAtIndexPath
方法中,我试图用以下行设置标签的文本:
cell.textLabel.text = [number objectAtIndex:indexPath.row];
+按钮有效,但-按钮无效。我该怎么解决这个问题?提前感谢!
蜂窝
- (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;
}
它将崩溃,因为该单元格与您的+和-按钮所在的单元格不同
您必须获得单击"+"或"-"按钮的"UITableViewCell"。然后根据需要操作文本。