检查每个单元格的条件
Posted
技术标签:
【中文标题】检查每个单元格的条件【英文标题】:conditions checking for every single cell 【发布时间】:2015-03-03 10:44:57 【问题描述】:我有一个表格视图,在表格单元格中我有两个按钮并检查它们的条件。它可以工作,但我的问题是我想检查表格视图中每个单元格的按钮条件。谁能帮帮我。
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal];
button1.tag = indexPath.row;
[button1 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button1];
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(160, 27, 36, 36);
[button2 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"e"ofType:@"png"]] forState:UIControlStateNormal];
button2.tag = indexPath.row;
[button2 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];
- (void)radiobtn:(UIButton *)button
if(btn3 == 0)
if ([button isSelected])
[button setImage:[UIImage imageNamed:@"l.png"]
forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"button1"];
[button setSelected:NO];
else
[button setImage:[UIImage imageNamed:@"lblue.png"]
forState:UIControlStateSelected];
[button setSelected:YES];
else
- (void)radiobtn3:(UIButton *)button
if(btn1 == 0)
if ([button isSelected])
[button setImage:[UIImage imageNamed:@"v.png"]
forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"button3"];
[button setSelected:NO];
else
[button setImage:[UIImage imageNamed:@"vblue.png"]
forState:UIControlStateSelected];
[button setSelected:YES];
else
条件运行良好。但我想。帮我写代码。
【问题讨论】:
您不应该在单元格中保留您正在检查的状态。由于单元格被重用,它们仅代表当时填充的数据。所以你的状态应该存储在填充单元格的对象中。 @rckoenes 我是 ios 新手,你能帮我写代码吗 【参考方案1】:您需要根据 UITableView 的 cellForRowAtIndexPath 数据源方法中的 indexPath.row 为分配的每个按钮设置标签。 This link 可能会帮到你。
【讨论】:
我试过这没有帮助,你能帮我编码吗 在开始制作 Objective C 应用程序之前,您需要了解 UITableView 及其各自的数据源和委托方法的基础知识。请阅读教程。。【参考方案2】:维护一个array
用于检查UIButton
的state
,这将是class member
。
为每个按钮添加#define
,以唯一地知道我们指的是哪个按钮状态,如下所示:
#define kFirstBtnState @"firstbuttonstate"
#define kSecondBtnState @"secondbuttonstate"
现在viewDidLoad
中的initialize
数组像这样:
//use tableview object array here
for(int i=0; i<[yourDataSourceOfTableViewArray count]; i++)
//Currently each button state is 0 which is false
NSDictionary *dictBtnState = [NSDictionary dictionaryWithObjectsAndKeys:@"0",kFirstBtnState,@"0",kSecondBtnState,nil];
[mutArrBtnStateMaintain addObject:dictBtnState];
像这样在UITableView's
cellForRowAtIndexPath
中使用创建的数组:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
....................
....................
//get state list of buttons
NSDictionary *dictBtnState = [mutArrBtnStateMaintain objectAtIndex:indexPath.row];
BOOL firstBtnState = [[dictBtnState objectForKey:kFirstBtnState] boolValue];
BOOL secondBtnState = [[dictBtnState objectForKey:kSecondBtnState] boolValue];
//Act according to your requirement
return cell;
注意:Replace
state
中array
必要时maintain
state
UIButton
中的state
【讨论】:
以上是关于检查每个单元格的条件的主要内容,如果未能解决你的问题,请参考以下文章