有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
当前回答
例子在swift 3..
Crease a single view application add tableview in view controller add a customcell for tablview cell view controller code is bellow like class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var tableView: UITableView! var arraytable = [[String:Any]]() override func viewDidLoad() { super.viewDidLoad() arraytable = [ ["title":"About Us","detail":"RA-InfoTech Ltd -A Joint Venture IT Company formed by Bank Asia Ltd"], ["title":"Contact","detail":"Bengal Center (4th & 6th Floor), 28, Topkhana Road, Dhaka - 1000, Bangladesh"] ] tableView.delegate = self tableView.dataSource = self //For Auto Resize Table View Cell; tableView.estimatedRowHeight = 44 tableView.rowHeight = UITableViewAutomaticDimension //Detault Background clear tableView.backgroundColor = UIColor.clear } func numberOfSections(in tableView: UITableView) -> Int { return arraytable.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } // Set the spacing between sections func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 10 } // Make the background color show through func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerView = UIView() headerView.backgroundColor = UIColor.clear return headerView } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as! CustomCell cell.tv_title.text = arraytable[indexPath.section]["title"] as! String? cell.tv_details.text = arraytable[indexPath.section]["detail"] as! String? //label height dynamically increase cell.tv_details.numberOfLines = 0 //For bottom border to tv_title; let frame = cell.tv_title.frame let bottomLayer = CALayer() bottomLayer.frame = CGRect(x: 0, y: frame.height - 1, width: frame.width, height: 1) bottomLayer.backgroundColor = UIColor.black.cgColor cell.tv_title.layer.addSublayer(bottomLayer) //borderColor,borderWidth, cornerRadius cell.backgroundColor = UIColor.lightGray cell.layer.borderColor = UIColor.red.cgColor cell.layer.borderWidth = 1 cell.layer.cornerRadius = 8 cell.clipsToBounds = true return cell } } Download full source to Github : link https://github.com/enamul95/CustomSectionTable
其他回答
我也有同感。起初,我试着换到分组课,但对我来说,最后比我最初想象的更头疼,所以我一直在寻找替代方案。为了继续使用行(并且不干扰你访问模型数据的方式),下面是我使用蒙版的方法:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
let verticalPadding: CGFloat = 8
let maskLayer = CALayer()
maskLayer.cornerRadius = 10 //if you want round edges
maskLayer.backgroundColor = UIColor.black.cgColor
maskLayer.frame = CGRect(x: cell.bounds.origin.x, y: cell.bounds.origin.y, width: cell.bounds.width, height: cell.bounds.height).insetBy(dx: 0, dy: verticalPadding/2)
cell.layer.mask = maskLayer
}
All you have left to do is make the cell's height bigger by the same value as your desired verticalPadding, and then modify your inner layout so that any views that had spacing to the edges of the cell have that same spacing increased by verticalPadding/2. Minor downside: you get verticalPadding/2 padding on both the top and bottom of the tableView, but you can quickly fix this by setting tableView.contentInset.bottom = -verticalPadding/2 and tableView.contentInset.top = -verticalPadding/2. Hope this helps somebody!
如果你不想使用任何标题,使用标题作为间距也可以。否则,这可能不是最好的主意。我在想的是创建一个自定义单元格视图。
例子:
用笔尖 在代码中
在自定义单元格中,创建一个带有约束的背景视图,这样它就不会填充整个单元格,给它一些填充。
然后,使tableview背景不可见,并删除分隔符:
// Make the background invisible
tableView.backgroundView = UIView()
tableView.backgroundColor = .clear
// Remove the separators
tableview.separatorStyle = .none
这篇文章很有帮助,它和其他答案差不多,但是总结和简洁
https://medium.com/@andersongusmao/left-and-right-margins-on-uitableviewcell-595f0ba5f5e6
其中,他只将它们应用于左侧和右侧,但UIEdgeInsetsMake初始化允许为所有四个点添加填充。
func UIEdgeInsetsMake(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> UIEdgeInsets Description Creates an edge inset for a button or view. An inset is a margin around a rectangle. Positive values represent margins closer to the center of the rectangle, while negative values represent margins further from the center. Parameters top: The inset at the top of an object. left: The inset on the left of an object bottom: The inset on the bottom of an object. right: The inset on the right of an object. Returns An inset for a button or view
注意,UIEdgeInsets也可以用来实现同样的效果。
Xcode 9.3/Swift 4
我需要做相同的概念,让UITableCells之间有一个“空间”。因为你不能在单元格之间添加空间,你可以通过操纵UITableView的单元格高度来伪造它,然后添加一个UIView到你的单元格的contentView。这是我在另一个测试项目中模拟的一个原型的屏幕截图:
下面是一些代码(注意:为了演示目的,有很多硬编码的值)
首先,我需要设置highightforrowatindexpath以允许UITableViewCell上的不同高度。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self.newsArray objectAtIndex:[indexPath row]];
if ([text isEqual:@"December 2012"])
{
return 25.0;
}
return 80.0;
}
接下来,我想操纵UITableViewCells的外观和感觉,所以我在willDisplayCell:(NewsUITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath方法中做到这一点。
- (void)tableView:(UITableView *)tableView willDisplayCell:(NewsUITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell.IsMonth)
{
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 20, 20)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:@"month-bar-bkgd.png"];
UILabel *monthTextLabel = [[UILabel alloc] init];
CGFloat font = 11.0f;
monthTextLabel.font = [BVFont HelveticaNeue:&font];
cell.backgroundView = av;
cell.textLabel.font = [BVFont HelveticaNeue:&font];
cell.textLabel.textColor = [BVFont WebGrey];
}
if (indexPath.row != 0)
{
cell.contentView.backgroundColor = [UIColor clearColor];
UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,70)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
whiteRoundedCornerView.layer.shadowOpacity = 0.5;
[cell.contentView addSubview:whiteRoundedCornerView];
[cell.contentView sendSubviewToBack:whiteRoundedCornerView];
}
}
注意,我使我的whiteRoundedCornerView高度70.0,这就是模拟空间的原因,因为单元格的高度实际上是80.0,但我的contentView是70.0,这给了它外观。
也许还有其他更好的方法,但这就是我找到的方法。我希望它能帮助到其他人。
不需要使用一堆不同的部分。其他的答案使用框架insets和CGRect和图层…等等等等。不太好;使用自动布局和自定义UITableViewCell。在那个UITableViewCell中,不是在contentView中查看你的内容,而是创建一个新的containerView(一个UIView),在contentView中查看容器视图,然后在容器视图中查看所有视图。
现在要创建空格,只需编辑容器视图的布局边距,如下所示:
class CustomTableViewCell: UITableViewCell {
let containerView = UIView()
let imageView = UIImageView()
required init?(coder aDecoder: NSCoder) {super.init(coder: aDecoder)}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
containerView.translatesAutoResizingMaskIntoConstraints = false
imageView.translatesAutoResizingMaskIntoConstraints = false
contentView.addSubview(containerView)
containerView.addSubview(imageView)
contentView.layoutMargins = UIEdgeInsets(top: 15, left: 3, bottom: 15, right: 3)
containerView.layoutMargins = UIEdgeInsets(top: 15, left: 17, bottom: 15, right: 17) // It isn't really necessary unless you've got an extremely complex table view cell. Otherwise, you could just write e.g. containerView.topAnchor
let cg = contentView.layoutMarginsGuide
let lg = containerView.layoutMarginsGuide
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: cg.topAnchor),
containerView.leadingAnchor.constraint(equalTo: cg.leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: cg.trailingAnchor),
containerView.bottomAnchor.constraint(equalTo: cg.bottomAnchor),
imageView.topAnchor.constraint(equalTo: lg.topAnchor),
imageView.leadingAnchor.constraint(equalTo: lg.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: lg.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: lg.bottomAnchor)
])
}
}