Ios 如何使UITableView看起来像这样

Ios 如何使UITableView看起来像这样,ios,iphone,objective-c,xcode,uitableview,Ios,Iphone,Objective C,Xcode,Uitableview,是否可以像在标准UITableView中那样在单元格之间设置距离?没有选择使分离器更大。还有这个距离从右到左 一种方法是将UITableViewCell的背景视图设置为在灰色背景上包含此白色框的UIImageView一种方法是将UITableViewCell的背景视图设置为在灰色背景上包含此白色框的UIImageView方法: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInde

是否可以像在标准UITableView中那样在单元格之间设置距离?没有选择使分离器更大。还有这个距离从右到左


一种方法是将UITableViewCell的背景视图设置为在灰色背景上包含此白色框的UIImageView

一种方法是将UITableViewCell的背景视图设置为在灰色背景上包含此白色框的UIImageView

方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     ...

  cell.backgroundColor = [UIColor whiteColor];

  // This will create a line of 3 pixels that will be separating the cells
  UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,3)];
  separator.backgroundColor = [UIColor darkGrayColor];
  [cell.contentView addSubview: separator];

  // and if you want the border do left and right add:
  UIView *separatorRx = [[UIView alloc] initWithFrame:CGRectMake(318,0,2,cell.frame.size.height)];
  separatorRx.backgroundColor = [UIColor darkGrayColor];
  [cell.contentView addSubview: separatorRx];

  UIView *separatorSx = [[UIView alloc] initWithFrame:CGRectMake(0,0,2,cell.frame.size.height)];
  separatorSx.backgroundColor = [UIColor darkGrayColor];
  [cell.contentView addSubview: separatorSx];

  return cell;

}
在方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     ...

  cell.backgroundColor = [UIColor whiteColor];

  // This will create a line of 3 pixels that will be separating the cells
  UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,3)];
  separator.backgroundColor = [UIColor darkGrayColor];
  [cell.contentView addSubview: separator];

  // and if you want the border do left and right add:
  UIView *separatorRx = [[UIView alloc] initWithFrame:CGRectMake(318,0,2,cell.frame.size.height)];
  separatorRx.backgroundColor = [UIColor darkGrayColor];
  [cell.contentView addSubview: separatorRx];

  UIView *separatorSx = [[UIView alloc] initWithFrame:CGRectMake(0,0,2,cell.frame.size.height)];
  separatorSx.backgroundColor = [UIColor darkGrayColor];
  [cell.contentView addSubview: separatorSx];

  return cell;

}

您必须创建
UITableViewCell
的子类。在你自己的子类中,你可以实现你梦想的一切!因此,您需要在自定义的
UITableViewCell
上添加
UIView
whiteColor
背景和边距作为子视图。您必须创建
UITableViewCell
的子类。在你自己的子类中,你可以实现你梦想的一切!因此,您需要在自定义的
UITableViewCell
中添加
UIView
背景和边距作为子视图,而
UITableView
中没有填充选项。您可以在包含所有单元格子视图的
UITableViewCell
中添加一个
UIView
,然后更改其框架以在单元格中创建一个填充


我建议您使用
UICollectionView
相反,您可以使用布局轻松设置单元格之间的间距。如果单元格小于实际的集合视图,则它将自动居中。

UITableView
中没有填充选项。您可以在包含所有单元格子视图的
UITableViewCell
中添加一个
UIView
,然后更改其框架以在单元格中创建一个填充


我建议您使用
UICollectionView
相反,您可以使用布局轻松设置单元格之间的间距。如果单元格小于实际的集合视图,则它将自动居中。

您可以通过将单元格背景设置为所需的任何背景(在本例中为灰色),然后在单元格中添加一个白色uiview,其左、右和下边距如下所示:

然后转到表视图,将分隔符设置为无

最后一步,将表格视图的背景设置为与单元格相同的灰色背景

然后,您不必在代码中做任何特别的事情,只需基于自定义nib文件初始化表单元格(我假设您无论如何都要这样做)


如果您喜欢在代码中构建单元格,可以使用相同的逻辑。

您可以通过将单元格背景设置为所需的任何背景(在本例中为灰色),然后在单元格中添加一个带有左、右和下边距的白色uiview,如下所示:

然后转到表视图,将分隔符设置为无

最后一步,将表格视图的背景设置为与单元格相同的灰色背景

然后,您不必在代码中做任何特别的事情,只需基于自定义nib文件初始化表单元格(我假设您无论如何都要这样做)


如果您喜欢在代码中构造单元格,可以使用相同的逻辑。

您需要自定义分隔符视图。尝试搜索。您需要自定义分隔符视图。尝试搜索。