ios - 仅两行 UITableView

标签 ios uitableview

我在带有一个部分的 Nib 上使用 UITableView 控件,并将委托(delegate)/数据源设置为我的 Controller ,其中“表数据”数组仅初始化为 2 个项目。 但是,在模拟器中运行时,我看到 TableView 中呈现了额外的空行。 如何让 UITableView 只呈现两行?

谢谢!

最佳答案

我遇到了类似的问题,如何仅显示包含数据的单元格的分隔符。 tableView 呈现尽可能多的行,以适应分配给它的区域(宽度和高度)。您在 numberOfRowsInSection 中指定的号码仅用于确定应该调用多少次cellForRowAtIndexPath .

为了克服空单元格分隔线的渲染,我执行了以下操作:

  • 禁用整个 tableView 的分隔符。您可以在界面生成器中的表格 View 的检查器中执行此操作,或调用 [yourTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; .
  • 在您的cellForRowAtIndexPath 内用单元格填充表格 View 的位置创建一个新的 UIView并将其设置为单元格的 subview 。使此 View 的背景为浅灰色且略微透明。您可以通过以下方式做到这一点:
    UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake:
                                    (0, cell.frame.size.height-1, 
                                       cell.frame.size.width, 1)];
    [separatorView setBackgroundColor:[UIColor lightGrayColor]];
    [separatorView setAlpha:0.8f];
    [cell addSubView:separatorView];
    

    此 View 的宽度为 1 像素,与默认分隔符相同,它在底部运行单元格的长度。

  • 由于 cellForRowAtIndexPath 只在 numberOfRowsInSection 中指定的频率被调用,这些 subview 只会为拥有数据并应该有分隔符的单元格创建。

    希望这可以帮助。

    关于ios - 仅两行 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10889567/

    相关文章:

    ios - UITableView 被释放 IOS SWIFT 随机崩溃

    ios - ios App中全局模型的使用

    iphone - iOS - UITableView 非静态背景图片

    iphone - 为 UITableView 过滤内容

    iphone - 如何在状态栏上添加按钮或 View

    ios - Tab+NavigationController流程

    ios - CustomCell 中的 TextField Change 事件

    ios - Xcode:AccessoryType.checkmark 将其他 View 推到左侧

    ios - swift : How can I use delete and share button in Cell?

    ios - iOS UITableViewCell 的 userData