1

Due to some permission issue, in my tableView some cell can be selected by user and some cell can't be selected by that user. What I did in my cellForRowAtIndexPath is:

    -(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    int row  = (int)[indexPath row];
    static NSString *simpleTableIdentifier = @"EditProjectTableCell";

    Project* project = (Project*)[self.projectList objectAtIndex:row];   
    EditProjectTableCell *cell = (EditProjectTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:simpleTableIdentifier owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.serial.layer.cornerRadius = 5;
        cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"accessory_right.png"]];
        cell.cellView.backgroundColor = [UIColor blackColor];
        cell.backgroundColor = [UIColor blackColor];
    }

    if ([project.role isEqualToString:@"1"] || [project.role isEqualToString:@"2"]) {
        UIView *customView = [[UIView alloc] initWithFrame:cell.frame];
        customView.backgroundColor = [UIColor clearColor];
        cell.selectedBackgroundView = customView;
    }
    else cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if(cell.imageFetchOperation != nil) [cell.imageFetchOperation cancel];
    cell.imageFetchOperation = [[CellBlockOperation alloc] initWithIndexPath:indexPath
                                                                     andPkId:project.pkId
                                                               andFetchImage:YES
                                                       andFetchReportCounter:NO
                                                        andFetchIssueCounter:NO
                                                      andFetchDrawingCounter:NO
                                                                 andDelegate:self];

    if(cell.counterFetchOperation != nil) [cell.counterFetchOperation cancel];
    cell.counterFetchOperation = [[CellBlockOperation alloc] initWithIndexPath:indexPath
                                                                       andPkId:project.pkId
                                                                 andFetchImage:NO
                                                         andFetchReportCounter:YES
                                                          andFetchIssueCounter:NO
                                                        andFetchDrawingCounter:NO
                                                                   andDelegate:self];


    cell.projectName.text = project.name;
    cell.projectNumber.text = project.number;
    cell.owner.text =  [NSString stringWithFormat:@"%@ %@",project.ownerName,project.ownerLastName];
    NSString* temp_date = [Utils stringFromDateForGUI:project.creationDate];
    cell.date.text = temp_date;
    [cell.syncStatusView setImage:[Utils getImageForSyncStatus:project.isDirty.intValue]];

    NSNumber *ret = [self.reportCountList objectForKey:project.pkId];
    if(ret == nil) {
        cell.serial.text = @"";
        [cell.counterFetchOperation setPkId:project.pkId];
        [self.tableCellUpdateQueue addOperation:cell.counterFetchOperation];
    } else {
        cell.serial.text = [NSString stringWithFormat:@"%d",[ret intValue]];
    }

    id img = [self getSmallImage:project.pkId andIndexPath:indexPath andCell:cell];
    if(img == nil || img == (id)[NSNull null]) {
        img = [UIImage imageNamed:@"gray-img.jpg"];
        cell.iconImageView.image = [UIImage imageNamed:@"project-icon.png"];
        cell.iconImageView.hidden = NO;
    } else {
        cell.iconImageView.hidden = YES;
    }
    cell.imageView.image = (UIImage*)img;

    if ([project.role isEqualToString:@"2"] || [project.role isEqualToString:@"1"])
        cell.selectImageView.hidden = NO;
    else
        cell.selectImageView.hidden = YES;

    return cell;
 }

Selection part is working just fine. But if a cell is selected then the background color of a UILabel gets changed to clear color. See the below images for clear idea.

Before selection:

enter image description here

After Selection :

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • Can we get a bit more code? Something doesn’t seem right if you always set style to none – Roy Falk Jan 18 '18 at 18:16
  • @RoyFalk see the edit. – Rashad Jan 22 '18 at 05:28
  • It's hard to understand what's happening here and what code is related to the digit 1 on the right side but I'm going to take a guess. When you select a cell, everything moves right. There are two options: 1. If the digit is overlaid on the rounded square (two separate elements) and there's a black layer half way through, it could place itself between them. 2. The rounded square is moved outside the cell to the right. My suggestion, create a second cell type for the selection and drop the NIBs. That way you'll see exactly how the elements are laid out. HTH – Roy Falk Jan 22 '18 at 06:11
  • Or it could be simply that this is the intended behavior :) You're reusing an existing imageView that's part of the UITableViewCell and it may become hidden on selection. See https://stackoverflow.com/questions/11681273/uitableviewcell-imageview-changing-on-select – Roy Falk Jan 22 '18 at 06:19
  • The rounded square is a label, it's background is gray and text is set to 1. cell.serial is that label. @RoyFalk – Rashad Jan 22 '18 at 06:22
  • https://stackoverflow.com/questions/7053340/why-do-all-backgrounds-disappear-on-uitableviewcell-select – Roy Falk Jan 22 '18 at 07:15
  • Possible duplicate of [Why do all backgrounds disappear on UITableViewCell select?](https://stackoverflow.com/questions/7053340/why-do-all-backgrounds-disappear-on-uitableviewcell-select) – Roy Falk Jan 22 '18 at 07:16

0 Answers0