开发者

Changing specific cells in uitableview

开发者 https://www.devze.com 2023-04-12 12:54 出处:网络
I have a uitableview but I want to disable some cells and leave others enabled. I used the following code to disable all cells except for the first in the cellForRowAtIndexPath:

I have a uitableview but I want to disable some cells and leave others enabled. I used the following code to disable all cells except for the first in the cellForRowAtIndexPath:

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

But instead of disabling all cells and enabling the first, it disabled all cells and enables the last. Why does this happen? And what can I do to get it to work?

BTW Here's my code for all cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIn开发者_如何学CdexPath:(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;
}


Replace your code of cellForRowAtIndexPath: with this one

- (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;
}


You don't show where you declare cell, but what you are doing is setting the userinteractionEnabled before getting the actual cell!

You have to put this customization code at the end of the method.

Also, try to declare the cell variable in the method, it has not to be used elsewhere.

- (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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号