ios - 如何通过点击UIButton IOS7来更新UILabel值

标签 ios objective-c uibutton uilabel

嗨,在我的应用程序中,我正在 UITableView 中显示本地数据库 sqlite 中的数据。在该数据中,我的 UILabel 中显示了整数值。现在想要通过单击按钮来增加减少值,我已经尝试过,但它无法正常工作,只有当再次单击下一行时,它才会显示旧值请告诉我如何解决这个问题。我动态创建了两个 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]: 
 }

最佳答案

问题在于,每次执行 tableView:cellForRowAtIndexPath: 代码时,您都会不断创建按钮。当您第一次调用 reloadData 时,有足够的单元格可供重用,因此您的代码会在旧按钮之上添加新按钮;每次单击 [+][-] 时都会发生这种情况。

为了解决此问题,请根据条件创建按钮。为按钮指定不同的标签 - 例如,[+] 为 100,[-] 为 101。这可以让您通过标签找到 subview ,并避免在新按钮已经存在时创建新按钮。由于您需要在 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];
}

关于ios - 如何通过点击UIButton IOS7来更新UILabel值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24385436/

相关文章:

ios - 谁能告诉我为什么我在执行此代码(Swift)时收到错误的身份验证错误?

ios - 从方法获取 CLLocation 坐标

ios - ld : warning: ignoring file

ios - 用于 Objective-C 和 Cocoa 的 CPAN/gem-like 存储库?

ios - NSXMLParserErrorDomain 111

ios - 为动态创建的 UIButtons 选择/取消选择

ios - 我想从渐变层的触摸点着色,如何?

ios - UIView 动画完成 block 立即执行

ios - 如何在swift 3中仅在按钮的有界区域上获取触摸事件

swift - 使用 AutoLaout 向 UIButton 添加 CAShapeLayer 未正确显示