UITableview按钮在滚动条上突出显示 [英] UITableview buttons getting highlighted on scroll

查看:73
本文介绍了UITableview按钮在滚动条上突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在tableview单元格中有切换按钮,并在某些单元格上单击它们,但是当我向下滚动时,即使我还没有选择,也为底部单元格选择了相同的按钮.我知道这是由于tableview重用单元格而发生的...有什么办法可以解决这个问题?

I have toggle buttons in my tableview cells and I click them on for some cells but when I scroll down, those same buttons are selected for the bottom cells as well even though I didn't select them yet. I know this is happening because of the tableview reusing cells...is there any way I can fix this?

单元是动态的,而不是静态的.

The cells are dynamic, not static.

tableview的外观

what the tableview looks like

**另外,lemme知道我的逻辑是否还好:我尝试在viewcontroller类中创建一个可变数组,然后将其所有值设置为@"0".然后,在tableviewcell的类中,将数组中的值设置为@"1".如果我选择按钮,则在当前单元格的索引处,然后返回到我的viewcontroller类,可以判断是否已经在该单元格处选择了按钮.唯一的缺点是我无法访问tableviewcell的类中的数组,它以null出现……我想这是由于目标c中的mvc模式.有什么建议吗?

** Also, lemme know if my logic seems alright: I tried creating a mutable array in my viewcontroller class and then setting all it's values to @"0". Then, in my tableviewcell's class, I set the value in the array to @"1" at the index of the current cell if I select the button, so then back in my viewcontroller class, I can tell if I have already selected a button at that cell or not. The only flaw is that I can't access the array in my tableviewcell's class, it is coming out at null...i guess that it because of the mvc pattern in objective c. Any advice?

编辑

我仍然无法解决我的问题.有人可以帮帮我吗?我已经被卡住了一段时间了!

I am still unable to resolve my issue. Can someone please help me? I have been stuck on it for a while now!

我正在尝试创建一个表格视图,其中的单元格具有复选和十字按钮,当我单击"check"按钮时,它应变为绿色,但是其他单元格中的相同按钮应保持灰色,但是当我向下滚动时,一些我没有选择按钮的单元格仍然变成绿色...由于单元格回收.

I am trying to create a tableview where the cells have a check and cross button and when I click the check button, it should turn green, but the same button in other cells should remain gray, however, when I scroll down, some cells that I didn't select buttons in still turn green...because of cell recycling.

我现在正在使用委托和协议,但是没有用;也许我用错了吗?

I am using delegates and protocols right now but it isn't working; perhaps I am using it wrong?

我正在我的单元类的IBaction函数中设置yesChecked值,并且在我的viewcontroller类中,我正在使用yesChecked值根据按钮是否显示是"来查看按钮的颜色.或否".

I am setting yesChecked value in IBaction functions in my cell class, and in my viewcontroller class, I am using that yesChecked value to see what color to give to the button based on whether it says "yes" or "no".

请帮助!谢谢!

@protocol DetailsTableViewCellDelegate <NSObject>

- (void) customCell:(DetailsTableViewCell *)cell yesBtnPressed:(bool)yes;

@property (nonatomic, retain) NSString * yesChecked;

推荐答案

您必须在cellForRowAt中选择或取消选择它们.例如,如果您的单元格具有leftButton属性,并且您具有这样的模型,则可以执行以下操作:

You'd have to select or deselect them in cellForRowAt. For example if your cell had a leftButton property and you had a model like this, you could do something like the following:

@interface Model : NSObject

@property (nonatomic, assign) BOOL selected;

@end

@protocol CustomCellDelegate <NSObject>

- (void)cellActionTapped:(UITableViewCell *)cell;

@end

@interface CustomCell : UITableViewCell

@property (nonatomic, assign) BOOL leftButtonSelected;
@property (weak, nonatomic, nullable) id<CustomCellDelegate> delegate;

@end

// ModelViewController.h
@interface ModelViewController : UIViewController<CustomCellDelegate>

@end

// ModelViewController.m
@interface ViewController () {
    NSArray<Model*>* models;
}

@end

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
    ((CustomCell *)cell).delegate = self;
    ((CustomCell *)cell).leftButtonSelected = models[indexPath.row].selected;
    return cell;
}

- (void)cellActionTapped:(UITableViewCell *)cell {
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    // Update data source using (maybe) indexPath.row
}

这篇关于UITableview按钮在滚动条上突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆