且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

UITableview 按钮在滚动时突出显示

更新时间:2023-11-01 08:16:52

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
}