快速更改tableviewcell的宽度 [英] change width of tableviewcell swift

查看:316
本文介绍了快速更改tableviewcell的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用IB的tableView以及自定义单元格和原型单元格。

I have a tableView using IB with custom cells and prototype cells.

我正在尝试使单元格的宽度比tableView.frame短一些在左右两个角之间留一点空间。

I'm trying to make the cells a little shorter in width than the tableView.frame to leave a little space between the left and right corners.

var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
        cell.bounds = CGRectMake(10, self.tableView.frame.origin.y, 30, self.tableView.frame.size.height)
        cell.layer.bounds = CGRectMake(10, self.tableView.frame.origin.y, 30, self.tableView.frame.size.height)
        cell.textLabel?.bounds = CGRectMake(10, self.tableView.frame.origin.y, 30, self.tableView.frame.size.height)

更新:解释如何将subView添加到tableView的好例子。
http://natashatherobot.com/ios- frame-vs-bounds-resize-basic-uitableview-cell /

Update: here is a good example explaining how to add a subView to your tableView. http://natashatherobot.com/ios-frame-vs-bounds-resize-basic-uitableview-cell/

更新2:似乎没有简单的方法做到这一点。据我所知,有3种方法可以实现此目的:

Update 2: Looks like there isn't an easy way to do this. There are 3 ways of achieving this as far as I know:


  1. 为您的单元格添加一个经过舍入且较短的图像,该图像具有相同的精确度颜色并匹配您的背景。

  2. 您可以将tableViewCell子类化,然后使用layoutSubviews进行播放,这样可以在绘制单元格之前使其更短。我已经做到了,但是滚动性能糟透了。

  3. 最好的方法是完全抛弃tableView并用collectionView重新做。


推荐答案

tableview中的单元格应该与容器一样宽。

The cells in the tableview are supposed to be as wide as their container.

如果您需要单元格的宽度与表格视图的宽度不同,我建议将视图作为子视图添加到 cell.contentView 并根据需要将其显示为尽可能宽的视图,同时确保 contentView 具有清晰的背景,没有分隔符和所有分隔符(因此它似乎不存在) 。

If you need your cells to have a different width than the table view, I would suggest adding a view as subview to cell.contentView and make that view as wide as you need while making sure the contentView has clear background and no separator and all (so that it appears it is not there).

另一种解决方案是使 tableView 不如其 superview ,向其添加左/右填充。但是您会遇到一个问题,即在左侧和右侧的填充位置,您将无法滚动 tableView

Another solution would be to have the tableView not as wide as it's superview by adding the left/right padding to it. But the you would have the issue that on the left and right side, where the padding is, you won't be able to scroll the tableView

我认为使用 collectionView 的最干净的解决方案。它与 tableView 并没有什么不同,您可以配置单元格的整个大小,而不仅仅是高度。

I consider the cleanest solution to use a collectionView. It is not that much different than a tableView and you can configure the entire size of the cell, not just the height.

希望这可以帮助您解决问题。让我知道您是否需要更多帮助。

Hope this helps you fix your problem. Let me know if you need more help.

这篇关于快速更改tableviewcell的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆