1

在我的应用程序中,我在 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。

4

4 回答 4

5

您需要在点击索引处获取 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"];
                  }
              }
          }

希望它可能会有所帮助。

于 2013-09-12T13:02:25.137 回答
1

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

 UIButton* buttonClicked = (UIButton*) sender;
 lblHeart.tag = buttonClicked.tag;
于 2013-09-12T12:51:19.003 回答
0

尽管 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]

于 2013-09-12T13:19:06.240 回答
0

尝试这个:

将您添加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

于 2015-04-08T12:57:46.647 回答