gpt4 book ai didi

ios - uitableview:在同一行中,选定的按钮应自动选中,其余按钮应自动取消选中

转载 作者:行者123 更新时间:2023-12-01 18:37:12 25 4
gpt4 key购买 nike

我正在使用 UITableview 进行反馈表单,因为它使用自定义复选框进行选择。在 UITableviewcell 中,我为选项放置了四个静态按钮,例如 非常
不错
, 不错 , 平均 , 低于平均水平 .

我想要的是,我只想选择一个连续选中的按钮,如果我选择另一个自动选中的按钮,则应该取消选中前一个选中的按钮。

示例:在同一行假设如果我选择 很好首先我选择了平均 , 之前选择的 很好应该取消选中。

检查下面的我的代码以供引用:

这是在 cellforrowatindexpath

[cell.R1_BTN setImage:[UIImage imageNamed:@"Touch_G.png"] forState:UIControlStateNormal];
[cell.R1_BTN addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.R1_BTN.tag=1;

点击这里的事件..
-(void)BtnClicked:(id)sender
{
//Need Code Here..
}

shared my screen once :

更新代码供引用..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [GNM count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [GNM objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([[NM objectAtIndex:section] isKindOfClass:[NSArray class]])
{
return [[NM objectAtIndex:section] count];
}
else
{
return 1;
}

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

cell.FBName_LBL.text = [[NM objectAtIndex:indexPath.section] isKindOfClass:[NSArray class]]
? [[NM objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]
: [NM objectAtIndex:indexPath.section];

// below for assigning code action event..
....
....
...

}

我尝试使用标签,但我没有得到我想要的,请帮助我.. 在此先感谢。

最佳答案

我认为你应该使用模型来设置 UITableViewCell。您的模型的 .h 文件如下:

#import <Foundation/Foundation.h>

typedef enum : NSInteger {
UNKNOWN = 0,
VERY_GOOD = 1,
GOOD = 2,
AVERAGE = 3,
BELOW_AVERAGE = 4
}RangeMark;

@interface CellModel : NSObject

@property(nonatomic, assign) RangeMark range;

@end

.m 文件,如:
#import "CellModel.h"

@implementation CellModel

@end

比你应该用 .xib 文件初始化一个表格单元格看起来像:
enter image description here

及其 .h 文件,如:
#import <UIKit/UIKit.h>
#import "CellModel.h"

@interface TableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIButton *veryGoodButton;
@property (weak, nonatomic) IBOutlet UIButton *goodButton;
@property (weak, nonatomic) IBOutlet UIButton *averageButton;
@property (weak, nonatomic) IBOutlet UIButton *belowAverageButton;

- (void)setupCellWithModel:(CellModel*)model;

@end

它的 .m 文件如下:
#import "TableViewCell.h"

@implementation TableViewCell

- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

- (void)setupCellWithModel:(CellModel *)model {

if(model.range == VERY_GOOD) {
self.veryGoodButton.backgroundColor = [UIColor greenColor];
}
else if(model.range == GOOD) {
self.goodButton.backgroundColor = [UIColor blueColor];
}
else if(model.range == AVERAGE) {
self.averageButton.backgroundColor = [UIColor yellowColor];
}
else if(model.range == BELOW_AVERAGE) {
self.belowAverageButton.backgroundColor = [UIColor redColor];
}
}

- (void)prepareForReuse {
[super prepareForReuse];
self.veryGoodButton.backgroundColor = [UIColor lightGrayColor];
self.goodButton.backgroundColor = [UIColor lightGrayColor];
self.averageButton.backgroundColor = [UIColor lightGrayColor];
self.belowAverageButton.backgroundColor = [UIColor lightGrayColor];
}

@end

最后,您的 View Controller .h 文件应如下所示:
#import <UIKit/UIKit.h>
#import "TableViewCell.h"

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

和 .m 文件应如下所示:
#import "ViewController.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>{
NSMutableArray<CellModel*> *modelList;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.tableView.estimatedRowHeight = 600;
self.tableView.rowHeight = UITableViewAutomaticDimension;

modelList = [NSMutableArray<CellModel*> new];

for (int i=0; i<50; i++) {
CellModel *cellModel = [[CellModel alloc] init];
cellModel.range = UNKNOWN;
[modelList addObject:cellModel];
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {

TableViewCell *cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"TableViewCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

[cell setupCellWithModel:[modelList objectAtIndex:indexPath.row]];

cell.veryGoodButton.tag = indexPath.row;
cell.goodButton.tag = indexPath.row;
cell.averageButton.tag = indexPath.row;
cell.belowAverageButton.tag = indexPath.row;

[cell.veryGoodButton addTarget:self action:@selector(veryGood:) forControlEvents:UIControlEventTouchUpInside];

[cell.goodButton addTarget:self action:@selector(good:) forControlEvents:UIControlEventTouchUpInside];

[cell.averageButton addTarget:self action:@selector(average:) forControlEvents:UIControlEventTouchUpInside];

[cell.belowAverageButton addTarget:self action:@selector(belowAverage:) forControlEvents:UIControlEventTouchUpInside];

return cell;

return nil;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return modelList.count;
}

- (void) veryGood:(UIButton*)sender {
[modelList objectAtIndex: sender.tag].range = VERY_GOOD;
[self setCellDynamicly:[NSIndexPath indexPathForRow:sender.tag inSection:0] withCellModel:[modelList objectAtIndex: sender.tag]];
}

- (void) good:(UIButton*)sender {
[modelList objectAtIndex: sender.tag].range = GOOD;
[self setCellDynamicly:[NSIndexPath indexPathForRow:sender.tag inSection:0] withCellModel:[modelList objectAtIndex: sender.tag]];
}

- (void) average:(UIButton*)sender {
[modelList objectAtIndex: sender.tag].range = AVERAGE;
[self setCellDynamicly:[NSIndexPath indexPathForRow:sender.tag inSection:0] withCellModel:[modelList objectAtIndex: sender.tag]];
}

- (void) belowAverage:(UIButton*)sender {
[modelList objectAtIndex: sender.tag].range = BELOW_AVERAGE;
[self setCellDynamicly:[NSIndexPath indexPathForRow:sender.tag inSection:0] withCellModel:[modelList objectAtIndex: sender.tag]];
}

- (void)setCellDynamicly:(NSIndexPath*)indexPath withCellModel:(CellModel*)cellModel {
TableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell prepareForReuse];
[cell setupCellWithModel:cellModel];
}

@end

就这样 :)

最后应用程序看起来像:

enter image description here

关于ios - uitableview:在同一行中,选定的按钮应自动选中,其余按钮应自动取消选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51055158/

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