2014-01-29 4 views
0

Я загружаю табличное представление и хотел бы оторвать объект 2 и объект 4 и сделать их отключенными для взаимодействия с пользователем.Отключить ячейки в NSArray UITableView

В .h У меня есть

@property (nonatomic, strong) NSArray *list; 

и .m:

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     self.list = [[NSArray alloc] initWithObjects:@"Object 1", @"Object 2", @"Object 3", @"Object 4", @"Object 5", nil]; 


    } 

и

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 


    cell.textLabel.text = [list objectAtIndex:[indexPath row]]; 


    return cell; 
} 

Я пытался получать объект с помощью

объект = [ self.list objectAtIndex: 2], чтобы получить O bject 2, но ничего не делает.

Как мне это сделать?

ответ

1

textLabel a UILabel или UITextField? Именование оставляет меня в недоумении. Предполагая, что это UILabel, и вы хотите предотвратить взаимодействие с ячейкой (и элементами управления в ней), вы можете сделать следующее.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 


    cell.textLabel.text = [list objectAtIndex:[indexPath row]]; 


    if (indexPath.row == 1 || indexPath.row == 3) { 
     cell.userInteractionEnabled = NO; 
    } else { 
     cell.userInteractionEnable = YES; 
    } 

    return cell; 
} 

Или вы просто пытаетесь запретить пользователям выбирать ячейку? В этом случае вы, вероятно, должны рассмотреть переопределение tableView:willSelectRowAtIndexPath: в UITableViewDelegate.

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.row == 1 || indexPath.row == 3) { 
     return nil; 
    return indexPath; 
} 
+0

совершенное спасибо –

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