【问题标题】:How to pad a UITableView section header?如何填充 UITableView 部分标题?
【发布时间】:2023-03-16 14:52:01
【问题描述】:

如何在 UITableView 单元格的部分标题中添加一些填充?

我有这个:

我有一个 NSDictionary,它的键值包含一串时间。我将键值存储在 NSArray 中。

我使用的是 titleForHeaderInSection 而不是 viewForHeaderInSection。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{   
    return [schedule objectAtIndex:section];
}

但是,节标题一直向左对齐。我可以根据自己的喜好使用 UIView for viewForHeaderInSection,但是对于 titleForHeaderInSection 方法有什么办法吗?

【问题讨论】:

    标签: ios objective-c xcode


    【解决方案1】:

    我无法使用 titleForHeaderInSection 轻松做到这一点,因此我决定使用带有 viewForHeaderInSection 的自定义单元格。

    【讨论】:

      【解决方案2】:

      最好使用 tableView:viewForHeaderInSection

      - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
      return 30;
      }
      
          - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
            UIView *viewHeader = [UIView.alloc initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
            UILabel *lblHeadertitle = [[UILabel alloc] initWithFrame:CGRectMake(YOUR_LEFT_PADDING, YOUR_TOP_PADDING, 200, 20)];
            //customize lblHeadertitle
            lblHeadertitle.text = [schedule objectAtIndex:section];
            [viewHeader addSubview:lblHeadertitle];
      
            return viewHeader;
          }
      

      【讨论】:

      • 但是如何使用 viewForHeaderInSection 来显示不同的节标题?
      • 可以设置 lblHeadertitle.text = [schedule objectAtIndex:section];查看更新的答案
      • 不知道为什么,但是章节标题已经消失了。我尝试了多个填充都没有用
      • 别忘了设置 heightForHeaderInSection,查看更新答案
      • 部分标题文本由于某种原因未对齐。无论我尝试什么填充,它总是在同一个错误的位置。请看链接:s13.postimg.org/jmfqfueiv/Untitled.png
      【解决方案3】:

      根据此处显示的答案: 你的身高viewHeader = 30, lblHeadertitle = 20。

      在代码处:

      UILabel *lblHeadertitle = [[UILabel alloc] initWithFrame:CGRectMake(YOUR_LEFT_PADDING, YOUR_TOP_PADDING, 200, 20)];
      

      设置YOUR_TOP_PADDING=5,因为lblHeadertitleviewHeader的子视图,让它在viewHeader居中。

      希望对你有帮助。

      【讨论】:

      • 谢谢,但没有帮助,您的建议部分标题文本完全消失了
      【解决方案4】:

      我认为最简单的解决方案是将标签嵌套在视图中。请参阅下面的 Swift 2 答案:

      func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
      
          // Create the header view
          let headerView =  UIView.init(frame: CGRectMake(0, 0, self.view.frame.size.width, 30))
          headerView.backgroundColor = UIColor.lightGrayColor()
      
          // Create the Label 
          let label: UILabel? = UILabel(frame: CGRectMake(20, 0, 120, 30))
          label!.textAlignment = .Left
          label!.text = "Your Section Title"
      
          // Add the label to your headerview 
          headerView.addSubview(label)
      
          return headerView
      }
      

      然后,您的文本上有一个缩进为 20 的标题视图:-D

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-22
        • 1970-01-01
        相关资源
        最近更新 更多