Подтвердить что ты не робот

Изображение UITableViewCell не является отступом

Я создал 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;
}

Я также реализовал функцию indentationLevelForRowAtIndexPath:

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

    // Return the indentation
    return groupInfo.indentation;
}

Теперь я получаю следующее представление таблицы:

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

Мой вопрос: почему изображение не отступы в ячейке просмотра таблицы?

4b9b3361

Ответ 1

Я закончил с подклассифицированием UITableViewCell и переопределив метод layoutSubViews:

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

Теперь мой вид таблицы выглядит следующим образом:

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

Ответ 2

Я искал только отступ от изображения и успешно реализовал его в Swift в конце. Спасибо! Тем не менее, были некоторые проблемы с конверсией между Int и CGFloat. То же самое в Swift работало для меня следующим образом:

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

Ответ 3

Роберто опубликовал довольно приятное решение. Однако, если layoutSubviews вызывается несколько раз во время представления ячейки, содержимое содержимого ячейки направляется вправо.

Мне удалось решить проблему, добавив

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

перед вызовами вычисления кадров и заменой каждого

self.imageView.frame.origin.x 

к

imageViewXOrigin

переменная.