gpt4 book ai didi

ios - 单元格按钮的操作

转载 作者:行者123 更新时间:2023-11-29 03:22:25 25 4
gpt4 key购买 nike

我正在使用表格 View ,其中我在一个单元格上添加了 2 个按钮。下面是我使用的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"list-bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
tickbtn.tag = 200+indexPath.row;
[tickbtn setBackgroundImage:[UIImage imageNamed:@"ok_gray.png"]forState:UIControlStateNormal];
[tickbtn addTarget:self action:@selector(addshed:) forControlEvents:UIControlEventTouchUpInside];
tickbtn.frame = CGRectMake(220, 10, 30, 30);
[cell.contentView addSubview:tickbtn];
NSLog(@"tickbtn tag %ld",(long)tickbtn.tag);

crossbtn = [UIButton buttonWithType:UIButtonTypeCustom];
crossbtn.tag = 400+indexPath.row;
[crossbtn setBackgroundImage:[UIImage imageNamed:@"delete-gray.png"]forState:UIControlStateNormal];
[crossbtn addTarget:self action:@selector(removeshed:) forControlEvents:UIControlEventTouchUpInside];
crossbtn.frame = CGRectMake(250, 10, 30, 30);
[cell.contentView addSubview:crossbtn];
NSLog(@"tickbtn tag %ld",(long)crossbtn.tag);

return cell;
}

在tickbtn和crossbtn上我正在应用以下操作:-

-(IBAction)addshed:(UIControl *)sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag-200 inSection:0];
UITableViewCell *cell = (UITableViewCell*)[list_table cellForRowAtIndexPath:indexPath];
UIButton *check1 = (UIButton*)[cell.contentView viewWithTag:indexPath.row+200];
UIButton *check2 = (UIButton*)[cell.contentView viewWithTag:indexPath.row+400];
UIImageView *btnimg1 = [[UIImageView alloc] initWithImage:check1.currentBackgroundImage];
//UIImageView *btnimg2 = [[UIImageView alloc] initWithImage:check2.currentBackgroundImage];
NSLog(@"SHED LIST subviews: %@", btnimg1.image);
// Shed_data *sheddata = [[Shed_data alloc] init];
if (btnimg1.image == [UIImage imageNamed:@"ok_gray.png"]) {
//btnimg.image = [UIImage imageNamed:@"ok_gray.png"];
[check1 setBackgroundImage:[UIImage imageNamed:@"ok_green.png"]forState:UIControlStateNormal];
[check2 setBackgroundImage:[UIImage imageNamed:@"delete-gray.png"]forState:UIControlStateNormal];
[self addsheddata:sender];
NSLog(@"tickbtn tag %ld",(long)tickbtn.tag);
}
else if (btnimg1.image == [UIImage imageNamed:@"ok_green.png"])
{
[check2 setBackgroundImage:[UIImage imageNamed:@"delete-red.png"]forState:UIControlStateNormal];
[check1 setBackgroundImage:[UIImage imageNamed:@"ok_gray.png"]forState:UIControlStateNormal];
[self removesheddata:sender];

}


}
-(IBAction)removeshed:(UIControl*)sender
{
//.…………………….. My functionality
}

但在这两种情况下,只有当我按下单元格的按钮时,我才会获取最后一个单元格的标签值。请找到我的错误并帮助我解决它。您的帮助将非常感激。

最佳答案

试试这个对我来说效果很好。我刚刚用我的 Xcode 5 进行了测试。

修改:1. 我创建一个名为 _objects 的 NSMutableArray (_objects = [[NSMutableArray alloc]initWithObjects:@"one",@"two",@"thre", nil];)。并将其提供给我的 UITableView

2.给 tickBtn 和 crossBtn 一个不同的颜色,这样很容易看到。

3.将按钮按下函数更改为 UIControlUIButton-(IBAction)addshed:(UIButton *)sender 和 when button按下我捕获标签值并将其打印在控制台上。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

cell.textLabel.text = [_objects objectAtIndex:indexPath.row];

cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"list-bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
tickbtn.tag = 200+indexPath.row;
[tickbtn setBackgroundImage:[UIImage imageNamed:@"ok_gray.png"]forState:UIControlStateNormal];
[tickbtn setBackgroundColor:[UIColor blackColor]];
[tickbtn addTarget:self action:@selector(addshed:) forControlEvents:UIControlEventTouchUpInside];
tickbtn.frame = CGRectMake(220, 10, 30, 30);
[cell.contentView addSubview:tickbtn];
NSLog(@"tickbtn tag %ld",(long)tickbtn.tag);

crossbtn = [UIButton buttonWithType:UIButtonTypeCustom];
crossbtn.tag = 400+indexPath.row;
[crossbtn setBackgroundImage:[UIImage imageNamed:@"delete-gray.png"]forState:UIControlStateNormal];
[crossbtn addTarget:self action:@selector(removeshed:) forControlEvents:UIControlEventTouchUpInside];
crossbtn.frame = CGRectMake(250, 10, 30, 30);
[crossbtn setBackgroundColor:[UIColor greenColor]];

[cell.contentView addSubview:crossbtn];
NSLog(@"tickbtn tag %ld",(long)crossbtn.tag);

return cell;
}

-(IBAction)addshed:(UIButton *)sender {
NSLog(@"add shed %d",sender.tag);
}

-(IBAction)removeshed:(UIButton *)sender {
NSLog(@"remove %d",sender.tag);
}

新问题更新

Did you try with 10 or more cells and try with some continuous scroll?

结果是

enter image description here enter image description here

正如另一个答案所说

[cell addSubview:crossbtn];// -------- Change here ---------

就我所知,让我澄清一下。

contentView 是 UITableViewCell 的 subview 。 please review this reference在这里您可以看到 UITableViewCell 中实际上有 3 个 subview 。

关于ios - 单元格按钮的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20917348/

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