2013-08-27 2 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

Поместите всю реализацию про - 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; 

    } 

теперь его решить

Смежные вопросы