将1减去cell.textLabel.text会导致应用崩溃

时间:2012-01-18 02:40:30

标签: iphone ios xcode cocoa-touch uitableview

您可以在我的1中添加和减去cell.textLabel.text。我用这种方法加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];

+按钮有效,但 - 按钮不起作用。我怎么能解决这个问题?提前谢谢!

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;
}

1 个答案:

答案 0 :(得分:0)

它会崩溃,因为单元格与你的+和 - 按钮所在的单元格不同。
你必须得到“UITableViewCell”,你点击了“+”或“ - ”按钮。然后根据需要操作文本。