lim*_*mon 139 objective-c uitableview ios swift
我想UITableView
为每个部分自定义标题.到目前为止,我已经实施了
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)
这种UITabelViewDelegate
方法.我想要做的是获取每个部分的当前标题,并添加UILabel
为子视图.
到目前为止,我无法做到这一点.因为,我找不到任何东西来获取默认节标题.第一个问题,有没有办法获得默认部分标题?
如果不可能,我需要创建一个容器视图,UIView
但是,这次我需要设置默认的背景颜色,阴影颜色等.因为,如果仔细查看部分的标题,它已经被自定义了.
如何为每个节标题获取这些默认值?
谢谢你们.
Loc*_*thy 285
你可以试试这个:
-(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;
}
Run Code Online (Sandbox Code Playgroud)
sam*_*ize 46
使用的所选答案tableView :viewForHeaderInSection:
是正确的.
只是在这里分享一个提示.
如果您正在使用storyboard/xib,那么您可以创建另一个原型单元并将其用于"section cell".配置标头的代码与为行单元配置的方式类似.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
static NSString *HeaderCellIdentifier = @"Header";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HeaderCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HeaderCellIdentifier];
}
// Configure the cell title etc
[self configureHeaderCell:cell inSection:section];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
est*_*oza 29
Swhana版本的Lochana Tejas回答:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 18))
let label = UILabel(frame: CGRectMake(10, 5, tableView.frame.size.width, 18))
label.font = UIFont.systemFontOfSize(14)
label.text = list.objectAtIndex(indexPath.row) as! String
view.addSubview(label)
view.backgroundColor = UIColor.grayColor() // Set your background color
return view
}
Run Code Online (Sandbox Code Playgroud)
Mer*_*ert 17
如果使用默认标题视图,则只能更改其上的文本
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)
对于Swift:
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
Run Code Online (Sandbox Code Playgroud)
如果您想自定义视图,则需要创建一个新视图.
如果未显示headerInSection,可以尝试此操作.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 45;
}
Run Code Online (Sandbox Code Playgroud)
这将返回给定节的标题的高度.
这是最简单的解决方案。以下代码可直接用于创建自定义节标题。
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
SectionHeaderTableViewCell *headerView = [tableView dequeueReusableCellWithIdentifier:@"sectionHeader"];
//For creating a drop menu of rows from the section
//==THIS IS JUST AN EXAMPLE. YOU CAN REMOVE THIS IF-ELSE.==
if (![self.sectionCollapsedArray[section] boolValue])
{
headerView.imageView.image = [UIImage imageNamed:@"up_icon"];
}
else
{
headerView.imageView.image = [UIImage imageNamed:@"drop_icon"];
}
//For button action inside the custom cell
headerView.dropButton.tag = section;
[headerView.dropButton addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchUpInside];
//For removing long touch gestures.
for (UIGestureRecognizer *recognizer in headerView.contentView.gestureRecognizers)
{
[headerView.contentView removeGestureRecognizer:recognizer];
[headerView removeGestureRecognizer:recognizer];
}
return headerView.contentView;
}
Run Code Online (Sandbox Code Playgroud)
注意: SectionHeaderTableViewCell 是在 Storyboard 中创建的自定义 UITableViewCell。
小智 5
试试这个......
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
// Background view is at index 0, content view at index 1
if let bgView = view.subviews[0] as? UIView
{
// do your stuff
}
view.layer.borderColor = UIColor.magentaColor().CGColor
view.layer.borderWidth = 1
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特3版本的lochana和estemendoza答案:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x:0, y:0, width:tableView.frame.size.width, height:18))
let label = UILabel(frame: CGRect(x:10, y:5, width:tableView.frame.size.width, height:18))
label.font = UIFont.systemFont(ofSize: 14)
label.text = "This is a test";
view.addSubview(label);
view.backgroundColor = UIColor.gray;
return view
}
Run Code Online (Sandbox Code Playgroud)
另外,请注意您还必须实施:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100;
}
Run Code Online (Sandbox Code Playgroud)
其他答案在重新创建默认标题视图方面做得很好,但实际上并没有回答您的主要问题:
有什么办法来获取默认的节头?
有一种方法-只需tableView:willDisplayHeaderView:forSection:
在您的代表中实施即可。默认的标题视图将传递到第二个参数中,然后您可以将其转换为a UITableViewHeaderFooterView
,然后根据需要添加/更改子视图。
对象
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
// Do whatever with the header view... e.g.
// headerView.textLabel.textColor = [UIColor whiteColor]
}
Run Code Online (Sandbox Code Playgroud)
迅速
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
let headerView = view as! UITableViewHeaderFooterView
// Do whatever with the header view... e.g.
// headerView.textLabel?.textColor = UIColor.white
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
243199 次 |
最近记录: |