gpt4 book ai didi

iphone - 使用发送者标签在UIbutton单击时更新UITableViewCell的标签

转载 作者:行者123 更新时间:2023-12-01 17:41:20 26 4
gpt4 key购买 nike

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

- (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"]];
}

在这里,我要更新的lblHeart与UITableviewCell的HeartPressed事件中的btnHeart标记具有相同的标记。

最佳答案

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

希望对您有所帮助。

关于iphone - 使用发送者标签在UIbutton单击时更新UITableViewCell的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18764768/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com