2013-08-27 28 views
0

我所創建的表視圖動態,在的cellForRowAtIndexPath我已經加在小區中的一個自定義按鈕作爲一個子視圖UITableViewCell的問題,同時滾動

if ([deviceNameArray count]!=0) 
{ 
    pairButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    pairButton.frame = CGRectMake(230.0f, 10.0f, 70.0f, 25.0f); 
    [pairButton setTitle:@"Connect" forState:UIControlStateNormal]; 
    pairButton.titleLabel.textColor = [UIColor lightGrayColor]; 
    pairButton.titleLabel.textAlignment = NSTextAlignmentCenter; 
    pairButton.titleLabel.font = [UIFont fontWithName:@"arial" size:10]; 

    pairButton.tag = indexPath.row; 

    [pairButton addTarget:self action:@selector(connectToDevice:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell addSubview:pairButton]; 
    isPairButtonAdded = YES; 

} 

我已經改變了設備的按鈕標題斷開連接後成功與我的另一設備。現在的問題是,無論何時滾動我的表格斷開連接變成連接,但我不希望發生這樣的事情,我知道它由於細胞娛樂如何停止重新創建細胞。

+0

把整個implementa - tableView:cellForRowAtIndexPath: – Moxy

回答

2

您必須存儲在陣列連接和斷開裝置的記錄,然後你可以管理你的這樣

if ([deviceNameArray count]!=0) 
{ 
    pairButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    pairButton.frame = CGRectMake(230.0f, 10.0f, 70.0f, 25.0f); 

    if([device isConnected]) // Get object from array and check is it coonected or disconnected 
    { 
     [pairButton setTitle:@"Connect" forState:UIControlStateNormal]; 
    } 
    else 
    { 
     [pairButton setTitle:@"Disconnected" forState:UIControlStateNormal]; 
    } 
pairButton.titleLabel.textColor = [UIColor lightGrayColor]; 
pairButton.titleLabel.textAlignment = NSTextAlignmentCenter; 
pairButton.titleLabel.font = [UIFont fontWithName:@"arial" size:10]; 

pairButton.tag = indexPath.row; 

[pairButton addTarget:self action:@selector(connectToDevice:) forControlEvents:UIControlEventTouchUpInside]; 
[cell addSubview:pairButton]; 
isPairButtonAdded = YES; 

}

0

你不能在cellForRowAtIndexPath:做到這一點:

// dequeue cell 
if (!cell) { 
    // create cell 
} 
// create button 

相反,你必須創建在細胞內的按鈕創建塊。否則,每次單元出現在屏幕上時都會再次創建一個新按鈕。

0

只是我得到有效解決的問題,裝箱我的內部條件按鈕我在哪裏檢查

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

    if ([deviceNameArray count]!=0) 
    { 
     pairButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     pairButton.frame = CGRectMake(230.0f, 10.0f, 70.0f, 25.0f); 
     [pairButton setTitle:@"Connect" forState:UIControlStateNormal]; 
     pairButton.titleLabel.textColor = [UIColor lightGrayColor]; 
     pairButton.titleLabel.textAlignment = NSTextAlignmentCenter; 
     pairButton.titleLabel.font = [UIFont fontWithName:@"arial" size:10]; 

     pairButton.tag = indexPath.row; 

     [pairButton addTarget:self action:@selector(connectToDevice:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell addSubview:pairButton]; 
     isPairButtonAdded = YES; 

    } 

現在它的解決