2016-03-11 135 views
-2

我正在構建一個調查應用程序,其中包含一個顯示問題的表格視圖。一些行具有文本字段,一些具有滑塊,一些具有5個按鈕供用戶選擇他們的答案。viewWithTag UIButton不能在tableview上工作reloadData

我可以設置初始UIButton的值很好,具有多個按鈕單元格,每個按鈕都可以設置其標題。但是,當我更改數據模型並重新加載tableview時,第一行(0)起作用,但第二行(1)不起作用。 button1.tag爲第二行返回0。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell forIndexPath:indexPath]; 

NSString *subCatKey = [NSString stringWithFormat:@"%ld", indexPath.row]; 
NSDictionary *thisSubCat = [_subCatListDict objectForKey:subCatKey]; 

NSString *defaultResponse = @""; 

static NSString *CellIdentifier; 
    CellIdentifier = @"buttonCell"; 

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
UIButton *button1 = [[UIButton alloc] init]; 
UIButton *button2 = [[UIButton alloc] init]; 
UIButton *button3 = [[UIButton alloc] init]; 
UIButton *button4 = [[UIButton alloc] init]; 
UIButton *button5 = [[UIButton alloc] init]; 

NSLog(@"Button 1::%ld",(long)button1.tag); 

// - 4 = Button range 
NSLog(@"Setting up the button cell"); 

NSArray *buttonResponses = [[NSArray alloc] initWithArray:[thisSubCat objectForKey:@"responses"]]; 

[buttonResponses objectAtIndex:0]; 

if(cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
} 

NSLog(@"Button tag 1::%ld",(long)button1.tag); 
if (button1.tag == 0) { 
    NSLog(@"Setting buttons..."); 
    button1 = (UIButton *)[cell viewWithTag:41]; 
    NSLog(@"Button tag 1::%ld",(long)button1.tag); 
    button2 = (UIButton *)[cell viewWithTag:42]; 
    button3 = (UIButton *)[cell viewWithTag:43]; 
    button4 = (UIButton *)[cell viewWithTag:44]; 
    button5 = (UIButton *)[cell viewWithTag:45]; 
    [button1 setTitle:[buttonResponses objectAtIndex:0] forState:UIControlStateNormal]; 
    [button2 setTitle:[buttonResponses objectAtIndex:1] forState:UIControlStateNormal]; 
    [button3 setTitle:[buttonResponses objectAtIndex:2] forState:UIControlStateNormal]; 
    [button4 setTitle:[buttonResponses objectAtIndex:3] forState:UIControlStateNormal]; 
    [button5 setTitle:[buttonResponses objectAtIndex:4] forState:UIControlStateNormal]; 

    [button1 addTarget:self 
       action:@selector(rowButtonSelect:) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [button2 addTarget:self 
       action:@selector(rowButtonSelect:) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [button3 addTarget:self 
       action:@selector(rowButtonSelect:) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [button4 addTarget:self 
       action:@selector(rowButtonSelect:) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [button5 addTarget:self 
       action:@selector(rowButtonSelect:) 
     forControlEvents:UIControlEventTouchUpInside]; 
} 


UILabel *questionLabel = (UILabel *)[cell viewWithTag:301]; 
questionLabel.text = [thisSubCat objectForKey:@"question"]; 

UILabel *questionResponses = (UILabel *)[cell viewWithTag:302]; 
questionResponses.text = defaultResponse; 
NSLog(@"Default response:%@",defaultResponse); 


return cell; 

}

+0

嘗試刪除if(button1.tag == 0)條件,或者在條件變爲false時添加else部分。願這能幫助你。 –

+0

您創建該按鈕,然後檢查其標籤。當然標籤值是零 - 這是它的默認值。目前尚不清楚你在這裏做什麼。此外,這是一個陳腐的方式來設置細胞。 – AMayes

+0

@ Dev.RK我添加了if(button1.tag == 0),因爲表從不滾動,它們不應該被重用。最初,我沒有這個,但是我認爲如果單元格沒有被重用,我會嘗試跳過它。 – jreynolds

回答

0

你總是建立在電池新按鈕,它的默認標籤= 0,這樣

NSLog(@"Button tag 1::%ld",(long)button1.tag); 

將打印按鈕標籤1 :: 0

設置

button1.tag indexpath.raw; 

並嘗試

+0

對,真正的測試是:button1 =(UIButton *)[cell viewWithTag:41];在reloadData之後,在第二個單元格(第一行)button1.tag仍然是0,儘管第0行單元格中的button1等於41,就像它應該是。 – jreynolds