【发布时间】:2011-11-03 14:36:15
【问题描述】:
我想要表格视图中两个单元格之间的空格,
我想要这样的细胞,
我该怎么做?
【问题讨论】:
标签: iphone objective-c cocoa-touch uitableview ios4
我想要表格视图中两个单元格之间的空格,
我想要这样的细胞,
我该怎么做?
【问题讨论】:
标签: iphone objective-c cocoa-touch uitableview ios4
您也可以在 UITableView 中创建 TableView 的 Sections... 此方法是强制性的,因此创建 section 并且在每个 section 中您可以像在图片中一样创建单个单元格..
【讨论】:
对于屏幕截图中的单元格之间的间距,不需要自定义单元格(无论如何,对于它们的外观,如渐变 bkg 等,这无论如何都是一个好主意,但这对你的单元格之间的间距)
要实现这种间距,只需在 UITableView 中使用不同的部分。
[编辑] 解释了所有内容 In Apple's TableView Programming Guide(值得一读,因为它包含很多关于 tableview 的知识)
【讨论】:
在 Table View DataSource 中有两个方法,分别是节数和行数 在部分 返回 3; 成行 返回 1;
【讨论】:
在 TableView 中获取两个单元格之间空间的最佳方法,以这种方式在 numberofsections 的委托方法中声明您想要的部分数
例如你有 10 个对象的数组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [array count]; //array count returns 10
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;// this should be one because it will create space between two cells if you want space between 4 cells you can modify it.
}
那么重点是在 cellForRowAtIndexPath 委托方法中你需要使用 indexpath.section 而不是 indexpath.row
cell.textLabel.text=[NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.section]];
即检查您的表格视图中两个单元格之间的空间。尽情享受吧!
【讨论】:
你不能直接设置单元格之间的距离,但是你可以在section中设置header的高度来达到同样的效果。
1.设置你需要的单元格数量:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3; // in your case, there are 3 cells
}
2.每个部分只返回1个单元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
3.设置部分标题的高度以设置单元格之间的空间
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10.; // you can have your own choice, of course
}
4.设置header的背景色为清色,这样不会显得怪怪的
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor clearColor];
return headerView;
}
【讨论】:
heightForHeaderInSection 中添加 if/else 语句。对于数组问题,只需使用indexPath.section 作为数组索引(而不是indexPath.row)。你说的根本不是问题。
有时,您可能真的希望将 tableview 保持在行中,并有 1 个部分。例如,如果您需要为该表格视图显示一个自定义标题,该标题在您滚动浏览该部分时保持在原位,则可能会发生这种情况。
在这种情况下,我建议做的是返回比单元格的正常高度更大的浮点数:
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
然后确保表格样式为普通,并且单元格分隔符为无。您可以在 XIB 文件本身或代码中执行此操作:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.style = UITableViewStylePlain;
也许还可以将单元格的选择样式添加为无(否则看起来您选择的不仅仅是单元格的可见部分)。
cell.selectionStyle = UITableViewCellSelectionStyleNone;
这会给单元格之间留下空间的印象,但同时将它们保持为一个部分中的行(这有时是您想要的)。
【讨论】:
对于寻找在不使用部分的情况下显示单元格之间间隙的替代方法的人,您可能希望显示替代颜色和高度,如下所示。使用clearColor 作为间距。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
if (indexPath.row % 2 == 1)
{
static NSString *CellIdentifier = @"cellID1";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWhite];
return cell;
} else {
static NSString *CellIdentifier2 = @"cellID2";
UITableViewCell *cell2 = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell2 == nil) {
cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier2];
}
cell2.backgroundColor = [UIColor clearColor];
return cell2;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 1) {
return 40.0;
} else {
return 2.0;
}
}
【讨论】:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [menus count]; }
多个部分的答案会起作用,但它非常脆弱,并且不允许 actual 部分。相反,您应该创建一个自定义单元格,或自定义单元格原型,在底部和/或顶部有一个间隙。
使用 IB 中的支柱和弹簧来保持均匀的间隙,并使用 heightForRowAtIndexPath 返回包含间隙的高度。
【讨论】:
您要在视觉上实现的目标与将每个单元格的内容添加到具有灰色背景的容器视图中并将该视图包含在单元格中相同。我认为没有必要在单元格之间添加空格。
【讨论】:
如果有人在寻找 Swift 版本。给你。
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 10; // space b/w cells
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return items.count // count of items
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView()
header.userInteractionEnabled = false
header.backgroundColor = UIColor.clearColor()
return header
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
【讨论】:
我所做的只是简单地通过创建一个比我想要的(单元格 + 间隙/2)大一点的简单自定义单元格来模拟空间,然后将所有单元格的内容放在内部视图中。
然后将单元格的最顶层视图作为背景颜色,将内部视图作为实际单元格。而且您会产生单元格之间有空间的错觉,而实际上它只是一个带有边框的较大单元格。
【讨论】:
目标 - C
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 3)];/// change size as you need.
separatorLineView.backgroundColor = [UIColor whiteColor];// you can also put image here
[cell.contentView addSubview:separatorLineView];
Swift 3(这同样适用于 Swift 4)
var separatorLineView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 3))
/// change size as you need.
separatorLineView.backgroundColor = UIColor.white
// you can also put image here
cell.contentView.addSubview(separatorLineView)
它对我有用。
【讨论】:
我为此使用了一种简单快捷的方法(使用故事板)
加载表格视图时,感觉单元格之间有空格。
【讨论】:
我已经检查了所有答案,但我认为这是最简单的方法:
UIView * theContentView = [UIView alloc]initWithFrame:CGRectMake(0,gap,width,height)];
theContentView.backgroundcolor = [UIColor lightGrayColor];//contentColor
cell.backgroundcolor = [UIColor blackColor];//gapColor
[cell addSubview: theContentView]
原型代码说你可以创建一个子视图来显示单元格的内容,剩下的就是你想要的间隙。
【讨论】:
我不知道为什么所有的答案都那么复杂。 KIS,仅使用情节提要,我在 tableCell 内容视图中放置了一个 UIView;现在 UIView 高度小于 Content View 高度,就是这样!
使用 Content View 颜色和 UIView 颜色来获得所需的结果。
【讨论】:
我使用 Swift 的简单解决方案:
// Inside UITableViewCell subclass
override func layoutSubviews() {
let f = contentView.frame
let fr = UIEdgeInsetsInsetRect(f, UIEdgeInsetsMake(10, 10, 10, 10))
contentView.frame = fr
}
或一行代码
override func layoutSubviews() {
contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, UIEdgeInsetsMake(10, 10, 10, 10))
}
【讨论】:
heightForRowAtIndexPath 中提供更高的高度,然后添加插入@UtkuDalmaz
在返回单元格之前,在cellForRowAtIndexPath UITableViewDelegate 方法中添加这些行。
let separator = UIView(frame: CGRectMake(0, 0, cell!.bounds.size.width, 1))
separator.backgroundColor = UIColor.whiteColor()
cell.contentView.addSubview(separator)
【讨论】:
您不必为每个单元格分配每个部分。只需在您的单元格内创建一个 UIView (容器),将其与单元格的视图边缘化。我们在该容器上布局标签、文本、图像等组件。
【讨论】:
* 使用 IOS 9 XCODE 7.3 *
实现此目的最直接的方法是将此代码添加到您的 cellForRowAtIndexPath 方法中。
cell.separatorInset.left = 20.0
cell.separatorInset.right = 20.0
cell.separatorInset.top = 20.0
cell.separatorInset.bottom = 20.0
cell.layer.borderWidth = 3
cell.layer.cornerRadius = 20.0
cell.layer.borderColor = UIColor.flatSkyBlueColorDark().CGColor
然后转到您的故事板并单击表格视图。转到身份检查器并将视图的背景颜色更改为方法中设置的任何边框颜色。瞧!使用这些值来获得所需的输出。希望这会有所帮助!
注意:如果使用 Chameleon 库,您必须在代码中设置视图的背景颜色,而不是通过故事板插件。由于某种原因,颜色似乎偏离了阴影。
【讨论】:
我建议创建一个自定义的UITableViewCell 基类并像下面这样使用这个类,
UITableViewCell 类UIView,这将充当“baseContentView”,它将是“UITableViewCell.contentView”的直接子代UITableViewCell.contentView) 调整“baseContentView”上的顶部“padding”(这将是分隔符/间隙)UITableViewCell
baseContentView”而不是“self.contentView”您可以根据需要使用“padding”。
【讨论】:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return __titlesArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header = [[UIView alloc]init];
header.backgroundColor = [UIColor clearColor];
return header;
}
【讨论】:
我用喜欢:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 194.0;
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.contentView.backgroundColor = UIColor.clearColor()
let whiteRoundedView : UIView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 185))
whiteRoundedView.backgroundColor = UIColor( red: CGFloat(61.0/255.0), green: CGFloat(117.0/255.0), blue: CGFloat(147.0/255.0), alpha: CGFloat(1.0))
whiteRoundedView.layer.masksToBounds = false
whiteRoundedView.layer.cornerRadius = 3.0
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1)
whiteRoundedView.layer.shadowOpacity = 0.5
cell.contentView.addSubview(whiteRoundedView)
cell.contentView.sendSubviewToBack(whiteRoundedView)
}
从以下位置获取颜色代码 RGB 值:
【讨论】:
1) 首先在表格视图中创建 2 个部分。 2)创建一个空单元格。 3)使用要显示的数据创建单元格。 4)使用方法
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section==0) {
if (indexPath.row % 2 != 1) { 返回 15.0; } 别的 { 返回 100; } } 否则
if (indexPath.row % 2 != 1) {
return 100.0;
} else {
return 15.0;
}
}
它将在单元格之间添加空间。它对我有用。
【讨论】:
这是我使用 Swift 3 的方法:
在 ViewDidLoad() 中,添加:
self.tableView.rowHeight = 500.0
在表格视图“CellForRowAt”中,添加以下内容:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
// Configure the cell...
var actualContentView = UIView()
actualContentView.frame = CGRect(x: 10, y: 10, width: cell.contentView.frame.width - 20, height: cell.contentView.frame.height - 20)
actualContentView.backgroundColor = UIColor.blue
cell.contentView.addSubview(actualContentView)
return cell
}
【讨论】:
在你的cellForRowAt
cell.layer.borderWidth = 2
cell.layer.cornerRadius = 10
cell.layer.borderColor = UIColor.systemBackground.cgColor
【讨论】:
最好的方法是使用 xib 文件并对其进行顶部和底部约束。例如,这里我给底部约束 10 并确保给单元格提供完美的高度,如下所示。代码中的“joinTableViewCell”是 xib 文件的文件名。
extension JoinTableViewController: UITableViewDataSource,UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("joinTableViewCell", owner: self, options: nil)?.first as! joinTableViewCell
cell.ImgView.layer.masksToBounds = true
cell.ImgView.cornerRadius(cell.ImgView.bounds.width / 2)
cell.lblName.text = "Christian Bell"
cell.lblJoin.text = "Joined"+"\t"+"Sep 3"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 90
}
}
【讨论】: