Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image of UITableViewCell is not indenting

I Have created a default UITableViewCell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Init empty cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GroupCellIdentifier];

    // Get the group info
    GroupInfo *groupInfo = (GroupInfo *)[_sortedGroupInfos objectAtIndex:[indexPath row]];

    // Check if we have initialized this cell before
    if(cell == nil)
    {
        // Initialize cell
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:GroupCellIdentifier];

        // Set the accessory type
        cell.accessoryType = UITableViewCellAccessoryNone;

        // Set the selection type
        cell.selectionStyle = [groupInfo.groupType isEqualToString:GroupTypePersonal] ? UITableViewCellSelectionStyleDefault : UITableViewCellSelectionStyleNone;

        // Set the indentation width
        cell.indentationWidth = 40;
    }

    // Set the label enabled status depending on the group type
    cell.textLabel.enabled = [groupInfo.groupType isEqualToString:GroupTypePersonal];
    cell.detailTextLabel.enabled = [groupInfo.groupType isEqualToString:GroupTypePersonal];

    // Set text
    cell.textLabel.text = groupInfo.description;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d %@", groupInfo.storeCount, [[Model instance] getTranslationFromSection:kSectionStoreSettings translationKey:@"Stores"]];

    // Set the image depending on the group type
    cell.imageView.image = [GroupInfo getImageForGroupType:groupInfo.groupType];

    // Return the cell
    return cell;
}

I also implemented the indentationLevelForRowAtIndexPath function:

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get the group info
    GroupInfo *groupInfo = _sortedGroupInfos[[indexPath row]];

    // Return the indentation
    return groupInfo.indentation;
}

Now I get the following table view:

http://i40.tinypic.com/f3e63t.png

My question is: Why is the image not indenting in the table view cell?

like image 247
Robert Veringa Avatar asked Nov 05 '13 08:11

Robert Veringa


3 Answers

Roberto posted pretty nice solution. However if layoutSubviews called multiple times during cell presentation the cell's content drive away to the right.

I was able to resolve the issue by adding

static CGFloat imageViewXOrigin = 0;
if(imageViewXOrigin == 0)
    imageViewXOrigin = self.imageView.frame.origin.x;

before frames calculation calls, and substituting every

self.imageView.frame.origin.x 

to

imageViewXOrigin

variable.

like image 156
Vlad Soroka Avatar answered Sep 22 '22 06:09

Vlad Soroka


I ended up with subclassing UITableViewCell and overriding the layoutSubViews method:

- (void)layoutSubviews
{
    // Call super
    [super layoutSubviews];

    // Update the separator
    self.separatorInset = UIEdgeInsetsMake(0, (self.indentationLevel * self.indentationWidth) + 15, 0, 0);

    // Update the frame of the image view
    self.imageView.frame = CGRectMake(self.imageView.frame.origin.x + (self.indentationLevel * self.indentationWidth), self.imageView.frame.origin.y, self.imageView.frame.size.width, self.imageView.frame.size.height);

    // Update the frame of the text label
    self.textLabel.frame = CGRectMake(self.imageView.frame.origin.x + 40, self.textLabel.frame.origin.y, self.frame.size.width - (self.imageView.frame.origin.x + 60), self.textLabel.frame.size.height);

    // Update the frame of the subtitle label
    self.detailTextLabel.frame = CGRectMake(self.imageView.frame.origin.x + 40, self.detailTextLabel.frame.origin.y, self.frame.size.width - (self.imageView.frame.origin.x + 60), self.detailTextLabel.frame.size.height);
}

My table view now looks like:

http://tinypic.com/r/24y20if/5

like image 15
Robert Veringa Avatar answered Oct 15 '22 09:10

Robert Veringa


I was looking only for the image indentation and successfully implemented it in Swift in the end. Thank you! However, there were some issues with the conversion between Int and CGFloat, The same thing in Swift worked for me like this:

class CustomUITableViewCell: UITableViewCell
{
    override func layoutSubviews() {
    // Call super
    super.layoutSubviews();

    // Update the frame of the image view
    self.imageView!.frame = CGRectMake(self.imageView!.frame.origin.x + (CGFloat(self.indentationLevel) * self.indentationWidth), self.imageView!.frame.origin.y, self.imageView!.frame.size.width, self.imageView!.frame.size.height);
    }
}
like image 6
Peter Ivanics Avatar answered Oct 15 '22 09:10

Peter Ivanics