【问题标题】:Image of UITableViewCell is not indentingUITableViewCell 的图像不缩进
【发布时间】:2013-11-05 08:14:13
【问题描述】:

我已经创建了一个默认的 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

我的问题是:为什么图像在表格视图单元格中没有缩进?

【问题讨论】:

    标签: objective-c image uitableview ios7 indentation


    【解决方案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】:

        Roberto 发布了非常好的解决方案。 但是,如果在单元格呈现期间多次调用 layoutSubviews,则单元格的内容会向右移动。

        我能够通过添加解决问题

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

        在帧计算调用之前,并替换每个

        self.imageView.frame.origin.x 
        

        imageViewXOrigin
        

        变量。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-09-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-05
          • 2021-12-24
          相关资源
          最近更新 更多