2015-03-03 70 views
0

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

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

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

+1

你不应该保持状态,这要检查,在单元格中。由于单元格被重用,它们仅代表填充的数据。所以你的状态应该存储在填充单元格的对象中。 – rckoenes 2015-03-03 10:49:00

+0

@rckoenes我是新来的ios你能帮助我在编码 – Raj 2015-03-03 10:57:31

回答

0

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

+0

我想这是没有帮助,u能帮助我在编码 – Raj 2015-03-03 10:55:42

+1

您需要了解的UITableView及其相应的数据源和委托方法的基础跳进使目标C之前应用程序..请通过教程.. – 2015-03-03 10:59:45

+0

http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/ – 2015-03-03 11:00:59

0

维护一个array检查stateUIButton这将是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'scellForRowAtIndexPath像这样的:

- (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; 
} 

注意Replacestatearray必要时maintainstateUIButton

相关问题