在我的应用程序中,我在UITableviewCell中动态创建动态UILabel和UIButton。他们的标签是UITableviewCell indexPath.row。我想在UIButton的click事件上更新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。
答案 0 :(得分: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"];
}
}
}
希望它可能会有所帮助。
答案 1 :(得分:1)
您可以将发件人转发给特定的类并阅读标记。
UIButton* buttonClicked = (UIButton*) sender;
lblHeart.tag = buttonClicked.tag;
答案 2 :(得分: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]
答案 3 :(得分: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