我正在使用UITableview进行反馈,即使用自定义复选框进行选择。在UITableviewcell中,我为四个选项放置了四个静态按钮,例如, 好,好,平均值,低于平均值。
我要的是,我只想选择连续选中的一个按钮,如果我自动选择了另一个按钮,则应该取消选中先前选择的按钮。
示例:在同一行中,假设如果我再次选择非常好,又选择了平均值,则应取消选中先前选择的非常好。< / p>
在下面检查我的代码以供参考:
这在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..
}
更新的代码以供参考。
- (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..
....
....
...
}
我尝试使用标签,但是我没有得到想要的东西,请帮我..谢谢提前。
答案 0 :(得分:1)
我认为您应该使用模型来设置UITableViewCell。您模型的.h文件,例如:
%td
.m文件,例如:
#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
及其.h文件,如:
#import "CellModel.h"
@implementation CellModel
@end
其.m文件,例如:
#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
最后,您的视图控制器.h文件应如下所示:
#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
和.m文件应如下所示:
#import <UIKit/UIKit.h>
#import "TableViewCell.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
仅此而已:)
最终,应用程序如下:
答案 1 :(得分:1)
是的,这将为您提供解决方案,至少我希望这样:)
首先创建一个自定义按钮.h文件,如下所示:
#import <UIKit/UIKit.h>
@interface CustomButton : UIButton
@property (assign) NSInteger sectionTag;
@property (assign) NSInteger rowTag;
@end
自定义按钮.m文件如下:
#import "CustomButton.h"
@implementation CustomButton
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
然后我像这样更改TableViewCell .h文件中的一些内容:
#import <UIKit/UIKit.h>
#import "CellModel.h"
#import "CustomButton.h"
@interface TableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet CustomButton *veryGoodButton;
@property (weak, nonatomic) IBOutlet CustomButton *goodButton;
@property (weak, nonatomic) IBOutlet CustomButton *averageButton;
@property (weak, nonatomic) IBOutlet CustomButton *belowAverageButton;
@property (weak, nonatomic) IBOutlet UILabel *itemLabel;
- (void)setupCellWithModel:(CellModel*)model;
@end
TableViewCell .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];
}
[self.itemLabel setText:model.itemText];
}
- (void)prepareForReuse {
[super prepareForReuse];
self.veryGoodButton.backgroundColor = [UIColor lightGrayColor];
self.goodButton.backgroundColor = [UIColor lightGrayColor];
self.averageButton.backgroundColor = [UIColor lightGrayColor];
self.belowAverageButton.backgroundColor = [UIColor lightGrayColor];
}
及其.xib如下:
另一方面,像这样的CellModel .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;
@property(nonatomic, copy) NSString* itemText;
- (id)initWith:(NSString*)itemText withRangeMark:(RangeMark)range;
@end
及其.m文件是这样的:
#import "CellModel.h"
@implementation CellModel
- (id)initWith:(NSString*)itemText withRangeMark:(RangeMark)range {
self = [super init];
if(self) {
self.itemText = itemText;
self.range = range;
}
return self;
}
@end
最终查看控制器.h文件相同,但.m像这样:
#import "ViewController.h"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>{
NSMutableArray *modelList;
NSMutableArray<NSString*> *sectionTitleList;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight = 600;
self.tableView.rowHeight = UITableViewAutomaticDimension;
sectionTitleList = [NSMutableArray<NSString*> new];
[sectionTitleList addObject:@"RESERVATION"];
[sectionTitleList addObject:@"FRONT DESK"];
[sectionTitleList addObject:@"CASHIER"];
[sectionTitleList addObject:@"HOUSE KEEPING"];
modelList = [[NSMutableArray alloc] initWithCapacity: 4];
[modelList insertObject:[NSMutableArray arrayWithObjects:[[CellModel alloc] initWith:@"Service Speed" withRangeMark:UNKNOWN],[[CellModel alloc] initWith:@"Good Speed" withRangeMark:UNKNOWN],[[CellModel alloc] initWith:@"Confirmation Quality" withRangeMark:UNKNOWN],[[CellModel alloc] initWith:@"Quick Service in Reservetion" withRangeMark:UNKNOWN],nil] atIndex:0];
[modelList insertObject:[NSMutableArray arrayWithObjects:[[CellModel alloc] initWith:@"Check In" withRangeMark:UNKNOWN],[[CellModel alloc] initWith:@"Happy on Their Service" withRangeMark:UNKNOWN],[[CellModel alloc] initWith:@"Coutesey" withRangeMark:UNKNOWN],[[CellModel alloc] initWith:@"Quick Service at Check In" withRangeMark:UNKNOWN],nil] atIndex:1];
[modelList insertObject:[NSMutableArray arrayWithObjects:[[CellModel alloc] initWith:@"Front Office & Reception" withRangeMark:UNKNOWN],[[CellModel alloc] initWith:@"Overall Quality of Room" withRangeMark:UNKNOWN],[[CellModel alloc] initWith:@"Check" withRangeMark:UNKNOWN],[[CellModel alloc] initWith:@"Response Time" withRangeMark:UNKNOWN],nil] atIndex:2];
[modelList insertObject:[NSMutableArray arrayWithObjects:[[CellModel alloc] initWith:@"Room Decor" withRangeMark:UNKNOWN],nil] atIndex:3];
// [modelList addObject: [[CellModel alloc] initWith:@"Service Speed" withRangeMark:UNKNOWN]];
// [modelList addObject: [[CellModel alloc] initWith:@"Good Speed" withRangeMark:UNKNOWN]];
// [modelList addObject: [[CellModel alloc] initWith:@"Confirmation Quality" withRangeMark:UNKNOWN]];
// [modelList addObject: [[CellModel alloc] initWith:@"Quick Service in Reservetion" withRangeMark:UNKNOWN]];
// for (int i=0; i<5; i++) {
// CellModel *cellModel = [[CellModel alloc] init];
// cellModel.range = UNKNOWN;
// cellModel.itemText = @"";
// [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];
}
NSLog(@"section: %ld - row : %ld - item text : %@", (long)indexPath.section, (long)indexPath.row, ((CellModel*)[[modelList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]).itemText);
[cell setupCellWithModel:[[modelList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
((CustomButton*)cell.veryGoodButton).rowTag = indexPath.row;
((CustomButton*)cell.veryGoodButton).sectionTag = indexPath.section;
((CustomButton*)cell.goodButton).rowTag = indexPath.row;
((CustomButton*)cell.goodButton).sectionTag = indexPath.section;
((CustomButton*)cell.averageButton).rowTag = indexPath.row;
((CustomButton*)cell.averageButton).sectionTag = indexPath.section;
((CustomButton*)cell.belowAverageButton).rowTag = indexPath.row;
((CustomButton*)cell.belowAverageButton).sectionTag = indexPath.section;
[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;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[modelList objectAtIndex:section] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionTitleList.count;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:12]];
[label setTextColor:[UIColor whiteColor]];
NSString *string =[sectionTitleList objectAtIndex:section];
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor darkGrayColor]];
return view;
}
- (void) veryGood:(CustomButton*)sender {
((CellModel*)[[modelList objectAtIndex:sender.sectionTag] objectAtIndex:sender.rowTag]).range = VERY_GOOD;
[self setCellDynamicly:[NSIndexPath indexPathForRow:sender.rowTag inSection:sender.sectionTag] withCellModel:[[modelList objectAtIndex:sender.sectionTag] objectAtIndex:sender.rowTag]];
}
- (void) good:(CustomButton*)sender {
((CellModel*)[[modelList objectAtIndex:sender.sectionTag] objectAtIndex:sender.rowTag]).range = GOOD;
[self setCellDynamicly:[NSIndexPath indexPathForRow:sender.rowTag inSection:sender.sectionTag] withCellModel:[[modelList objectAtIndex:sender.sectionTag] objectAtIndex:sender.rowTag]];
}
- (void) average:(CustomButton*)sender {
((CellModel*)[[modelList objectAtIndex:sender.sectionTag] objectAtIndex:sender.rowTag]).range = AVERAGE;
[self setCellDynamicly:[NSIndexPath indexPathForRow:sender.rowTag inSection:sender.sectionTag] withCellModel:[[modelList objectAtIndex:sender.sectionTag] objectAtIndex:sender.rowTag]];
}
- (void) belowAverage:(CustomButton*)sender {
((CellModel*)[[modelList objectAtIndex:sender.sectionTag] objectAtIndex:sender.rowTag]).range = BELOW_AVERAGE;
[self setCellDynamicly:[NSIndexPath indexPathForRow:sender.rowTag inSection:sender.sectionTag] withCellModel:[[modelList objectAtIndex:sender.sectionTag] objectAtIndex:sender.rowTag]];
}
- (void)setCellDynamicly:(NSIndexPath*)indexPath withCellModel:(CellModel*)cellModel {
TableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell prepareForReuse];
[cell setupCellWithModel:cellModel];
}
@end
我认为它将对您很好。只需将代码上的数组初始化部分更改为动态:)
最后一次出现:
答案 2 :(得分:1)
class uploadfileform(forms.ModelForm):
class Meta:
model=uploadfolder
fields=('File_to_upload',)
也许这可以帮助您。
答案 3 :(得分:0)
由于您没有按要求提供API响应详细信息,因此我们被迫使用静态值。请从数组API响应中替换sectionTitleList和modelList。请找到下面的代码,用于将您的API响应分配给@GökhanAydın创建的模型。
sectionTitleList = @[@"RESERVATION",@"FRONT DESK",@"CASHIER",@"HOUSE KEEPING",@"COMMON"];
modelList = @[
@[
@"Service Speed",
@"Good Service",
@"Confirmation quality",
@"Quick Service in Reservation"
],
@[
@"Check In",
@"Happy on their Service",
@"Courtesey",
@"Quick Service at Checkin"
],
@[
@"Front office & reception",
@"Overall Quality of Room",
@"Check",
@"Response time"
],
@[
@"Room Decor",
@"Time taken to serveTime taken to serveTime taken t",
@"Bathroom",
@"Facilities in the Room",
@"Choice of menu",
@"Housekeeping",
@"Room Service"
],
@[
@"Overall Comments",
@"Will you come back again"
]
];
self.navigationItem.title = [modelList lastObject];
GNM = [sectionTitleList mutableCopy];
NM = [[NSMutableArray alloc]init];
for (NSArray *feedbacktitles in modelList) {
if ([feedbacktitles isKindOfClass:[NSArray class]]) {
__block NSMutableArray *tempArray = [NSMutableArray new];
[feedbacktitles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL * _Nonnull stop) {
FeedbackModel *model = [[FeedbackModel alloc]initWith:title withRangeMark:UNKNOWN];
[tempArray addObject:model];
if (idx == [feedbacktitles count] - 1 ) {
*stop = TRUE;
[self->NM addObject:tempArray];
tempArray = [NSMutableArray new];
}
}];
}
}
或通过简单的for循环
for (NSArray *feedbacktitles in modelList) {
NSLog(@"%@",feedbacktitles);
NSMutableArray* rowStringArray = [NSMutableArray new];
if ([feedbacktitles isKindOfClass:[NSArray class]]) {
for(int i = 0; i < [feedbacktitles count]; i++) {
NSString* rowTitle = [feedbacktitles objectAtIndex:i];
FeedbackModel *model = [[FeedbackModel alloc]initWith:rowTitle withRangeMark:UNKNOWN];
[rowStringArray addObject: model];
if (i == [feedbacktitles count] - 1) {
[NM addObject: rowStringArray];
rowStringArray = [NSMutableArray new];
}
}
}
}