Вычитание 1 в cell.textLabel.text приводит к сбою приложения - PullRequest
0 голосов
/ 18 января 2012

Вы можете сложить и вычесть 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 голосов
/ 18 января 2012

Сбой из-за того, что ячейка не совпадает с ячейкой, в которой находятся ваши кнопки + и -.
Вы должны получить «UITableViewCell», в котором вы нажали кнопку «+» или «-».А затем манипулируйте текстом так, как вам требуется.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...