gpt4 book ai didi

iphone - 如何在 uitableview 单元格中显示按钮,它们的操作将显示在同一单元格的标签上

转载 作者:可可西里 更新时间:2023-11-01 05:13:53 25 4
gpt4 key购买 nike

在 UITableViewCell 中有不同的按钮,当我点击任何按钮时,它的 Action 正在所有单元格上执行,但我希望当我按下按钮时, Action 将显示在同一个单元格的标签上,我我知道我需要将它们放在数组中,但是如何...?

当我点击按钮时,一个值会递增,它应该显示在同一个单元格的标签上这是代码:-

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

likeShow=[[UILabel alloc]initWithFrame:CGRectMake(0, 160, 80, 20)];
likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[likeBtn addTarget:self action:@selector(likeFn) forControlEvents:UIControlEventTouchUpInside];
likeBtn.frame=CGRectMake(0, 110, 90, 50);
[likeBtn setTitle:@"Like" forState:UIControlStateNormal];
[likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:likeBtn];
[cell addSubview:likeShow];
return cell;
}

这是按钮的 Action

-(void)likeFn{
NSString *str;
NSMutableString *mystring=[NSMutableString string];
likeCount++;
str =[NSString stringWithFormat:@"%d",likeCount];
NSString *world = @"Like";
NSString *helloWorld = [world stringByAppendingString:str];
likeShow.text=helloWorld;
}

最佳答案

试试下面的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



NSString *CellIdentifier = [NSString stringWithFormat:@"Cell-%d",indexPath.row];

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//[cell clearsContextBeforeDrawing];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;

int lbltag = 1000;

UIButton *likeBtn = nil;
UILabel *likeShow = nil;

if ([cell viewWithTag:lbltag])
{
likeShow = (UILabel*)[cell viewWithTag:lbltag];
}
else
{
likeShow=[[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)] autorelease];
likeShow.text = [NSString stringWithFormat:@"%d likes",[[_marrTest objectAtIndex:indexPath.row] intValue]];
likeShow.tag = lbltag;
[cell addSubview:likeShow];
}

if ([cell viewWithTag:indexPath.row+1])
{
likeBtn = (UIButton*)[cell viewWithTag:indexPath.row+1];
}
else
{
likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
likeBtn.tag = indexPath.row+1;
[likeBtn addTarget:self action:@selector(likeFn:) forControlEvents:UIControlEventTouchUpInside];
likeBtn.frame=CGRectMake(90, 0, 90, 50);
[likeBtn setTitle:@"Like" forState:UIControlStateNormal];
[likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:likeBtn];

}

return cell;
}

-(void)likeFn:(UIButton*)btnClicked
{
NSString *strLikes = [_marrTest objectAtIndex:btnClicked.tag-1];
int likeCount = [strLikes intValue] + 1;
[_marrTest replaceObjectAtIndex:btnClicked.tag-1 withObject:[NSString stringWithFormat:@"%d",likeCount]];

NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:btnClicked.tag-1 inSection:0];

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIndexPath];

UILabel *requiredLabel = (UILabel*)[cell viewWithTag:1000];

NSString *str = requiredLabel.text;

//str = [str stringByAppendingFormat:@"selected %@", str];
requiredLabel.text = @"";
requiredLabel.text = [NSString stringWithFormat:@"%d likes",[[_marrTest objectAtIndex:btnClicked.tag-1] intValue]];

//do what ever you want with the label
}

关于iphone - 如何在 uitableview 单元格中显示按钮,它们的操作将显示在同一单元格的标签上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14495686/

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