【问题标题】:Update label of UITableViewCell on the event of UIbutton click using sender tag使用发件人标签在 UIbutton 单击事件时更新 UITableViewCell 的标签
【发布时间】:2013-09-12 12:48:42
【问题描述】:

在我的应用程序中,我在 UITableviewCell 中动态创建动态 UILabel 和 UIButton。它们的标签是 UITableviewCell indexPath.row。我想在 UIButton 的单击事件上更新 UILabel,它具有相同的 UIButton 标签。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"UIGridViewRow";
    MyCustomCell *Cell  = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (Cell == nil)
    {
         btnHeart = [UIButton buttonWithType:UIButtonTypeCustom];
         [btnHeart setFrame:CGRectMake(150,350,20,20)];
         [btnHeart setImage:[UIImage imageNamed:@"heart.png"] forState:UIControlStateNormal];
         [btnHeart addTarget:self action:@selector(HeartPressed:) forControlEvents:UIControlEventTouchDown];
         btnHeart.tag = indexPath.row;
         [cell addSubview:btnHeart];

         lblHeart = [[UILabel alloc] initWithFrame:CGRectMake(175, 350, 20, 20)];
         [lblHeart setBackgroundColor:[UIColor clearColor]];
         [lblHeart setTextColor:[UIColor whiteColor]];
         [lblHeart setTag:indexPath.row];
         [lblHeart setText:[NSString stringWithFormat:@"%@",[[appdel.PhotoAry objectAtIndex:photo.tag] valueForKey:@"Hearts"]]];
         [cell addSubview:lblHeart];
    }
}

-(IBAction)HeartPressed:(id)sender
{
     urlString = [NSString stringWithFormat:@"http://zealousys.com/PeekaBoo_Webservice/heart.php?uid=%@&pid=%@",appdel.strUserid,[[appdel.PhotoAry objectAtIndex:[sender tag]] valueForKey:@"ImageID"]];

     NSURL *url = [[NSURL alloc]initWithString:urlString];
     NSURLRequest *request = [NSURLRequest requestWithURL:url];
     NSData *GETReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
     NSError *myError = nil;
     NSDictionary *res = [NSJSONSerialization JSONObjectWithData:GETReply options:NSJSONReadingMutableLeaves error:&myError];
     lblHeart.text = [NSString stringWithFormat:@"%@",[res valueForKey:@"Hearts"]];
}

这里我想在 UITableviewCell 的 HeartPressed 事件中更新与 btnHeart 标签相同标签的 lblHeart。

【问题讨论】:

  • 为标签设置新标题后,您需要调用 [yourTableview reloadData];
  • 这里我不想重新加载整个表格视图,我只想更新按钮事件上的特定单元格标签。
  • 您的代码很乱,您已将标签添加到 self.view 并在 tableviewcell 方法中添加了该代码??
  • sorry其实有cell但是self.view是我写错了。
  • 好的,告诉我一件事,你如何只更新一个单元格的标签?哪个是桌子的一部分?我认为不重新加载表格数据是不可能的,所以在更新标签文本后必须重新加载表格

标签: iphone ios uitableview uibutton


【解决方案1】:

您需要在点击索引处获取 UITableViewCell,然后您可以在 UITableViewCell 内更新标签

NSIndexPath *likedIndex = [NSIndexPath indexPathForRow:sender.tag inSection:0];

MyCustomCell *cell = (MyCustomCell *)[self.table cellForRowAtIndexPath:likedIndex];

          for (UIView *subView in cell.subviews) {

              if ([subView isKindOfClass:[UILabel class]]) {

                    //sender.tag = button tag
                  if (subView.tag == sender.tag) {

                      [(UILabel *) subView setText:@"Hearts"];
                  }
              }
          }

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    您可以将 sender 投射到特定的类并读取标签。

     UIButton* buttonClicked = (UIButton*) sender;
     lblHeart.tag = buttonClicked.tag;
    

    【讨论】:

    • 但是如何设置与 Uibutton 标签相同的 lblHeart 的文本
    【解决方案3】:

    虽然 GyanendraSingh 的回答部分不错,但您应该投 UITableViewCell

    MyCustomCell *cell = (MyCustomCell *)[self.table cellForRowAtIndexPath:likedIndex];
    

    另外,最好在MyCustomCell 中创建属性。如果你这样做,你的代码可能如下所示:

    MyCustomCell *cell = (MyCustomCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
    
    cell.lblHeart.text = @"Hearts";
    

    昨天早上刚做了这个。您不必拨打[self.tableView reloadData]

    【讨论】:

      【解决方案4】:

      试试这个:

      将您的 Label 添加到该 UIButton's accessibilityCustomActions 数组中:

      btnHeart.accessibilityCustomActions = [NSArray arrayWithObject: lblHeart];
      

      然后通过类型转换从发送方获取此控件:

      - (void)btnHeart_click:(id)sender
      {
          UIButton *btnHeart = (UIButton *)sender;
          DBG(@"%li", btnHeart.tag);
          NSArray *arrHeart = (NSArray *)btnHeart.accessibilityCustomActions;
          UILabel *lblHeart = (UILabel *)[arrHeart objectAtIndex:0]; // getting 0th index of `accessibilityCustomActions` array.
      
          lblHeart.text = @"500";
      }
      

      这样您将获得所需的UILabel,但在您更改appdel.PhotoAry's 中特定索引的NSDictionary 中的文本值之前,它不会显示更新的文本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 2014-05-15
        • 2016-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多