2015-03-03 85 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

相關問題