Изменение определенных ячеек в uitableview - PullRequest
0 голосов
/ 13 октября 2011

У меня есть uitableview, но я хочу отключить некоторые ячейки и оставить другие включенными.Я использовал следующий код, чтобы отключить все ячейки, кроме первой в cellForRowAtIndexPath:

if ([indexPath row] > 0 ) {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.userInteractionEnabled = NO;
    cell.textLabel.textColor = [UIColor lightGrayColor];
}

Но вместо того, чтобы отключить все ячейки и включить первую, он отключил все ячейки и включил последнюю.Почему это происходит?И что я могу сделать, чтобы заставить его работать?

Кстати, вот мой код для всех cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]];
    contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]];

    if ([indexPath row] > 0 ) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.userInteractionEnabled = NO;
        cell.textLabel.textColor = [UIColor lightGrayColor];
    }
    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = contentsForThisRow;
    return cell;
}

Ответы [ 2 ]

0 голосов
/ 14 октября 2011

Вы не показываете, где вы объявляете ячейку, но вы устанавливаете userinteractionEnabled до получения фактической ячейки!

Вы должны поместить этот код настройки в конец метода.

Также попробуйте объявить переменную ячейки в методе, она не должна использоваться где-либо еще.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]];
    contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]];

    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([indexPath row] > 0 ) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.userInteractionEnabled = NO;
        cell.textLabel.textColor = [UIColor lightGrayColor];
    }

    cell.textLabel.text = contentsForThisRow;
    return cell;
}
0 голосов
/ 14 октября 2011

Замените ваш код cellForRowAtIndexPath: на этот

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]];
    contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]];

    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1        reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = contentsForThisRow;
    if ([indexPath row] > 0 ) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.userInteractionEnabled = NO;
        cell.textLabel.textColor = [UIColor lightGrayColor];
    }
    return cell;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...