UITableview buttons getting highlighted on scroll(UITableview 按钮在滚动时突出显示)
问题描述
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.
what the tableview looks like
** EDIT: 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?
EDIT
I am still unable to resolve my issue. Can someone please help me? I have been stuck on it for a while now!
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?
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".
Kindly help! Thanks!
@protocol DetailsTableViewCellDelegate <NSObject>
- (void) customCell:(DetailsTableViewCell *)cell yesBtnPressed:(bool)yes;
@property (nonatomic, retain) NSString * yesChecked;
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 按钮在滚动时突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UITableview 按钮在滚动时突出显示


基础教程推荐
- 如何使 UINavigationBar 背景透明? 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- LocationClient 与 LocationManager 2022-01-01