iOS- 如何改变section header

希望这个从UITableViewDelegate协议里得到的方法可以对你有所帮助:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
if (section == integerRepresentingYourSectionOfInterest)
[headerView setBackgroundColor:[UIColor redColor]];
else
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}

使用任何你喜欢UIColor代替[UIColor redColor]。你可能还希望调整headerView的尺寸。


DoctorG
这是改变文本颜色的方法:

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
label.text = @"Section Header Text Here";
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];


whyoz
不要忘记从委托添加这段代码,否则在某些情况下视图将被切断或者出现在table后面,相对于视图/标签的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}


Leszek ?arna
如果你想自定义header颜色,可以这样做:

[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];

这个方法在iOS 6.0.以上都很好用。


Maulik
这是在标题视图添加图片的方法:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
UIImageView *headerImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-gery-bar.png"]] autorelease];

headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 30);

[headerView addSubview:headerImage];

return headerView;
}



William Jockusch
如果你不想建立自定义视图,你也可以这样改变颜色(需要在iOS6里):

-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
UIView* content = castView.contentView;
UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
content.backgroundColor = color;
}
}


Dj S
这是常见的问题,我认为答案需要更新一下。
这个方法不涉及定义和创建自定义视图。在iOS
6以上,你可以通过以下方法轻松改变背景色和文本色:

- (void)tableView:(UITableView *)tableView
willDisplayHeaderView:(UIView *)view
forSection:(NSInteger)section

委托方法
例如:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor blackColor];

// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];

// Another way to set the background color
// Note: does not preserve gradient effect of original header
// header.contentView.backgroundColor = [UIColor blackColor];
}



orbv
通过UITableViewHeaderFooterView设置背景色的方法已经被废弃了。请用contentView.backgroundColor代替。

iOS- 如何改变section header,码迷,mamicode.com

时间: 2024-08-05 11:16:21

iOS- 如何改变section header的相关文章

UITableView section header 不固定

iOS系统自带的UITableView,当数据分为多个section的时候,在UITableView滑动的过程中,默认section header是固定在顶部的,滑动到下一个section的时候,下一个section header把上一个section header顶出屏幕外.典型的应用就是通讯录. 默认情况下,UITableView的section header是固定的,如何让section header不固定呢?也就是随着UITableView的滑动而滑动,顶部不是一直都显示section

IOS UITableView Group&Section

UItableView 根据数据结构不同 会有不同样式 关键在两个代理 tableviewdelegate&tabledatasourse 下面代码是我实施的Group 在模拟器中 ios6.1和ios7 并且滚动后相应的section会“置顶”,效果不错哦! 核心代码: #import <UIKit/UIKit.h> @interface AnnouncementViewController : UIViewController<UITableViewDataSource,UI

IOS只改变父视图的的透明度,不会改变子View的透明度

//正在滚动 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ float offset_Y = scrollView.contentOffset.y; NSLog(@"%f",scrollView.contentOffset.y); UIView *barView = [self.navigationController.view viewWithTag:1000]; if (offset_Y == -20) { //col

ios不可改变NSString类和可改变NSMutableString类的使用

一, NSString的常用用法 1,创建对象两种方法 1)   "-"号方法  alloc + 初始化initWithString  NSString *str = [[NSString alloc] initWithString:@"iPhone 4"];  initWithFormat  这种方法比initWithString更强大可以设置格式,如上述的字符串" iPhone 4" 可以写成NSString *str = [[NSStrin

iOS:改变UITextField或UITextView的光标颜色

全局改变 [[UITextView  appearance] setTintColor:COLOR_WITH_RGB(226,233,253)]; [[UITextField  appearance] setTintColor:COLOR_WITH_RGB(226,233,253)];

iOS开发 改变UINavigationController的UINavigationBar的高度和背景图片

1.改变高度 自定义UINavigationBar的新类别: [cpp] view plaincopy //UINavigationBar+BackgoundImage.h #import <Foundation/Foundation.h> @interface UINavigationBar (BackgoundImage) @end 在新类别的实现中,覆盖原有类的方法 - (void)drawRect:(CGRect)rect : [cpp] view plaincopy //UINavi

设置TableView section header的颜色

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {    view.tintColor = [UIColor clearColor];}

iOS UITableView的Section Footer添加按钮

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源码下载:点我传送 在处理UITableView表格时,我们希望在View底部添加按钮. 用户拖动UITableView时按钮能跟随移动. 如题,实现如下界面: - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)sec

iOS:改变UITableViewCell的选中背景色

要改变UITableViewCell选中时的背景色,需要在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)方法中添加如下代码: // 设置cell选中的背景色 UIView *selectionColor = [[UIView alloc] init]; selectionColor.backgroundColor = [UIColor colorWithRed