如何通过单击UIButton IOS7更新UILabel值

时间:2014-06-24 11:28:22

标签: ios objective-c uibutton uilabel

您好我的应用程序我在sqlite中的本地数据库UITableView中显示数据。在该数据中,我在UILabel中显示整数值。现在要通过点击按钮我想要incrementdecrement值但是它没有正常工作它只工作一次再点击下一行它显示旧值请告诉我如何解决这个。我已经动态地为增量和减量创建了两个UIButtons

我的数据库数据获取代码。

   qtt=[[NSMutableArray alloc]init];
   [self openDB];
 NSString *sql =[NSString stringWithFormat:@"SELECT * FROM br"];
 sqlite3_stmt *statement;
 if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
     while (sqlite3_step(statement)== SQLITE_ROW) {

        char *field5 = (char *)sqlite3_column_text(statement, 4);
        NSString *field5Str =[[NSString alloc]initWithUTF8String:field5];

         str2 = [[NSString alloc]initWithFormat:@"%@",field5Str];

        [qtt addObject:str2];
      }

   }

我的UITableView显示代码。

   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       static NSString *cellIdentifier =@"Cell";

       displayCell *cell =(displayCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      if (cell == nil) {

         cell = [[displayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
       }

       cell.ttt.text= [self.qtt objectAtIndex:indexPath.row];

       button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       button.frame = CGRectMake(130,25, 40.0, 40.0);
       [button setTitle:@"+" forState:UIControlStateNormal];
       [button addTarget:self action:@selector(aMethod:)
       forControlEvents:UIControlEventTouchUpInside];
       button.tag=indexPath.row;
       [cell.contentView addSubview:button];

       button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       button1.frame = CGRectMake(200,25, 40.0, 40.0);
       [button1 setTitle:@"-" forState:UIControlStateNormal];
       [button1 addTarget:self action:@selector(aMethod1:)
       forControlEvents:UIControlEventTouchUpInside];
       button1.tag=indexPath.row;
       [cell.contentView addSubview:button1];
     if([valueString isEqualToString:[self.qtt objectAtIndex:indexPath.row]])
      {
         int value=[valueString  integerValue];
     if (self.check==YES)
       {
          value=value+1;
          str2=[NSString stringWithFormat:@"%d",value];
          NSLog(@"%@",str2);       
       }
       else
        {
           if (value==1)
        {

        }
        else
         {
             value=value-1;
             str2=[NSString stringWithFormat:@"%d",value];
             NSLog(@"%@",str2);

           }

        }
        cell.ttt.text=str2;

      }

      return cell;
   }

我的UIButton动作方法代码。

  -(void)aMethod:(id)sender
 {
   self.check=YES;
   int tag=[sender tag];
   valueString=[self.qtt objectAtIndex:tag];
   [self.mytableview reloadData];
  }
 -(void)aMethod1:(id)sender
 {
    self.check=NO;
    int tag=[sender tag];
    valueString=[self.qtt objectAtIndex:tag];
    [self.mytableview reloadData]: 
 }

1 个答案:

答案 0 :(得分:2)

问题是每次通过tableView:cellForRowAtIndexPath:代码时都会继续创建按钮。当您第一次调用reloadData时,有足够的单元可供重用,因此您的代码会在旧代码之上添加新按钮;每次点击[+][-]

时都会发生这种情况

要解决此问题,请按条件创建按钮。为您的按钮指定不同的标签 - 例如,[+]为100,[-]为101。这样您就可以通过标记找到子视图,并避免在它们已经存在时创建新按钮。由于您需要在aMethod:aMethod1:中添加该标记,请将indexPath.row的标记放在单元格本身上,并使用int tag = [sender.superview tag]表达式来检索它。

以下是如何实现这一目标的框架:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier =@"Cell";

    displayCell *cell =(displayCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[displayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.ttt.text= [self.qtt objectAtIndex:indexPath.row];

    if ([cell.contentView viewWithTag:100] == nil) {
        button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(130,25, 40.0, 40.0);
        [button setTitle:@"+" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(aMethod:)
        forControlEvents:UIControlEventTouchUpInside];
        button.tag=100;
        [cell.contentView addSubview:button];
    }
    if ([cell.contentView viewWithTag:101] == nil) {
        button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button1.frame = CGRectMake(200,25, 40.0, 40.0);
        [button1 setTitle:@"-" forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(aMethod1:)
        forControlEvents:UIControlEventTouchUpInside];
        button1.tag=101;
        [cell.contentView addSubview:button1];
    }
    cell.contentView.tag = indexPath.row;
    // Remove the increment/decrement code, it does not belong here
}

您的事件处理程序方法如下所示:

-(void)aMethod:(id)sender {
    int tag=[[sender superview] tag];
    int value=[[self.qtt objectAtIndex:tag] integerValue]+1;
    [self.qtt replaceObjectAtIndex:tag withObject:[@(value) stringValue]];
    [self.mytableview reloadData];
}

-(void)aMethod1:(id)sender {
    int tag=[[sender superview] tag];
    int value=[[self.qtt objectAtIndex:tag] integerValue]-1;
    if (value <= 0) value = 1;
    [self.qtt replaceObjectAtIndex:tag withObject:[@(value) stringValue]];
    [self.mytableview reloadData];
}