检查每个单元格的条件

时间:2015-03-03 10:44:57

标签: ios objective-c iphone uitableview uibutton

我有一个表视图,在表格单元格中我有两个按钮并检查它们的条件。它的工作但我的问题是我想检查表视图中每个单元格的按钮条件。谁能帮助我。

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 {
}

条件运作良好。但我想检查每个单元格的条件。帮我编码。

2 个答案:

答案 0 :(得分:0)

您需要为您分配的每个按钮设置标签,具体取决于您在UITableView的cellForRowAtIndexPath数据源方法中的indexPath.row。 This link可能会帮助你。

答案 1 :(得分:0)

维持一个array来检查state UIButton class member

为每个按钮添加#define,以便唯一知道我们所指的按钮状态:

#define kFirstBtnState @"firstbuttonstate"
#define kSecondBtnState @"secondbuttonstate"

现在initialize中的viewDidLoad数组就像这样:

//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