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