【问题标题】:How to customize tableview header according to my requirement?如何根据我的要求自定义表格视图标题?
【发布时间】:2018-05-29 03:57:26
【问题描述】:

我正在尝试实现一项功能:

图 1 显示带有标题的标题。当您单击标题 1、2、3、4 时,它将展开并显示 tableview 行,如图 2 所示。

我想要的是在标题 0 视图中,我想要自定义并想要添加图像和一些文本(参见图 3)。

如何实现这个功能,如何使用Objective-C自定义tableview header?

我使用了下面的代码,但没有显示任何更改,

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:12]];
     NSString *string =[list objectAtIndex:section];
    /* Section header is in 0th index... */
    [label setText:string];
    [view addSubview:label];
    [view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
    return view;
}

Here is my project sample

【问题讨论】:

  • 和单元格一样
  • 图片 2 是我创建的,我想创建一个标题视图。剩下的事情都做完了,只剩下那一部分了。
  • 查看此链接,iostute.com/2015/04/expandable-and-collapsable-tableview.html。在这个 viewForHeaderInSection: 函数中,他们实现了一些视图和像这样的超级线,你也做同样的事情。

标签: ios objective-c uitableview expandable-table


【解决方案1】:

使用viewForHeaderInSection 方法。它返回一个 UIView。您可以通过编程方式创建所需的 UIView()。我假设你知道该怎么做。

现在,在这个方法中检查indexPath.row。如果它的“0”那么你的 ImageView 应该返回......否则返回一些其他视图,例如带有特定文本的标签。希望对您有所帮助。

【讨论】:

  • @Zahurazful 你能检查一下我现在更新的项目代码吗。
  • @S Shekh...我似乎无法下载它..不知道为什么 :(您可以在代码中发布您编辑的图片吗?
  • 该链接工作正常,任何人都可以查看和下载。如果您无法下载,请分享您的电子邮件ID,我会尽快给您发邮件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
相关资源
最近更新 更多