UITableViewCell 上的按钮图像在滚动时被清除
我在 UITableView 的每个单元格中添加了三个带有背景图像的 UIButtons 作为单选按钮未选中的图像,当有人单击它时,按钮图像将更改为单选按钮选中的图像,但问题是当我滚动 UITableView 时,选中的按钮图像被清除当我滚动时。
有人可以给我任何想法吗..!
的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
static NSString *CellSetup = @"CellSetup";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellSetup] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag=[indexPath row];
myButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 20, 20)];
[myButton setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
myButton.tag = ++tagCount;
[cell.contentView addSubview:myButton];
tagCount++;
myButton2 = [[UIButton alloc]initWithFrame:CGRectMake(80, 20, 20, 20)];
[myButton2 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
[myButton2 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
myButton2.tag = tagCount;
[cell.contentView addSubview:myButton2];
tagCount++;
myButton3 = [[UIButton alloc]initWithFrame:CGRectMake(140, 20, 20, 20)];
[myButton3 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
[myButton3 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
myButton3.tag = tagCount;
[cell.contentView addSubview:myButton3];
return cell;}
这是我声明 UIButtons -(void)selectRadioButon:(id)sender {
btnTag = [sender tag];
NSArray *arr = self.view.subviews;
UITableView *tblCell = [arr objectAtIndex:0];
NSArray *cellAry = tblCell.subviews;
for (int i = 0; i <[cellAry count]; i++) {
UITableViewCell *content = [cellAry objectAtIndex:i];
NSArray *contentAry = content.contentView.subviews;
for (int j = 0; j <[contentAry count]; j++) {
UIButton *button = [contentAry objectAtIndex:j];
if (btnTag == button.tag) {
for (int k = 0; k <[contentAry count]; k++) {
UIButton *button = [contentAry objectAtIndex:k];
if (btnTag == button.tag) {
[button setImage:[UIImage imageNamed:@"radioselect.png"] forState:UIControlStateNormal];
}
else
[button setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
}
return;
}
}
}
I have added three UIButtons with background image as radio button unchecked image in the UITableView's each cell and when Somebody clicks it the buttons image will change to radio buttons checked image but the problem is when I am scrolling the UITableView the checked buttons images are getting cleared while I am scrolling.
Can somebody please give me any idea..!
This the code in which I have declared UIButtons
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
static NSString *CellSetup = @"CellSetup";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellSetup] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag=[indexPath row];
myButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 20, 20)];
[myButton setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
myButton.tag = ++tagCount;
[cell.contentView addSubview:myButton];
tagCount++;
myButton2 = [[UIButton alloc]initWithFrame:CGRectMake(80, 20, 20, 20)];
[myButton2 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
[myButton2 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
myButton2.tag = tagCount;
[cell.contentView addSubview:myButton2];
tagCount++;
myButton3 = [[UIButton alloc]initWithFrame:CGRectMake(140, 20, 20, 20)];
[myButton3 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
[myButton3 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
myButton3.tag = tagCount;
[cell.contentView addSubview:myButton3];
return cell;}
-(void)selectRadioButon:(id)sender {
btnTag = [sender tag];
NSArray *arr = self.view.subviews;
UITableView *tblCell = [arr objectAtIndex:0];
NSArray *cellAry = tblCell.subviews;
for (int i = 0; i <[cellAry count]; i++) {
UITableViewCell *content = [cellAry objectAtIndex:i];
NSArray *contentAry = content.contentView.subviews;
for (int j = 0; j <[contentAry count]; j++) {
UIButton *button = [contentAry objectAtIndex:j];
if (btnTag == button.tag) {
for (int k = 0; k <[contentAry count]; k++) {
UIButton *button = [contentAry objectAtIndex:k];
if (btnTag == button.tag) {
[button setImage:[UIImage imageNamed:@"radioselect.png"] forState:UIControlStateNormal];
}
else
[button setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
}
return;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用数组来存储indexpath.row以显示选中的复选框请参阅此代码
添加此方法作为您的复选框按钮的选择器并为您的按钮indexPath.row添加标题。
这为您提供了识别已选中复选框的单元格的方法。
并在 cellForRowAtIndexPath 方法中实现检查。
请参阅此检查条件
根据您的情况使用此并解决您的问题。
use an array for storing indexpath.row for showing chceked check boxes see this code
Add this method as your selector for your checbox button and add title for you button indexPath.row.
This provide you the way for identifying the cell which having checked check boxes.
And implement checks in cellForRowAtIndexPath method.
see this check conditions
Use this according to you and get your problem solved.