更改UITable部分backgroundColor而不丢失部分标题
我已经改变了我的tableviews部分的backgroundColor / backgroundImage。
我使用普通的tableView。 不用担心,这个代码没有问题的工作:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease]; sectionView.backgroundColor = [UIColor greenColor]; //For using an image use this: //sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]]; return sectionView; }
现在的问题是, 节标题不见了。
我用这个代码设置部分的标题:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){ return @""; } else { id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; return [sectionInfo name]; } }
如果我不使用,我正在获得部分标题
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
需要你的帮助。
非常感谢。
编辑1:
感谢Mutix:
在我的情况下,答案是:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 20)] autorelease]; sectionView.backgroundColor = [UIColor greenColor]; //sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]]; //Add label to view UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 20)]; titleLabel.backgroundColor = [UIColor clearColor]; //section Title if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){ titleLabel.text = @""; } else { id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; titleLabel.text = [sectionInfo name]; } [sectionView addSubview:titleLabel]; [titleLabel release]; return sectionView;
}
viewForHeaderInSection
重写titleForHeaderInSection
方法。
在使用viewForHeaderInSection
时,要为标题添加一个标题,你将需要像下面这样在节标题视图中添加一个标签:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease]; sectionView.backgroundColor = [UIColor greenColor]; //Add label to view UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 40)]; titleLabel.text = @"title"; [sectionView addSubview:titleLabel]; [titleLabel release]; return sectionView; }
从iOS6起,我们有更好的解决scheme:
有一个委托方法
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
你可以像这样使用这个方法
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { //Set the background color of the View view.tintColor = [UIColor blueColor]; // Text Color UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view; [header.textLabel setTextColor:[UIColor whiteColor]]; }
从UITableViewDelegate实现下面的方法,并需要ui视图。
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let view = UITableViewHeaderFooterView() view.contentView.backgroundColor = UIColor(white: 0.97, alpha: 1) return view }
如果您需要更改页脚,请使用… viewForFooterInSection ..方法中的类似代码
要绘制你自己的字体,使用类似的定制:
... let label = UILabel(frame: CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), tableView.sectionHeaderHeight)) label.font = UIFont(name: "HelveticaNeue", size: 14)! label.text = self.tableView(tableView, titleForHeaderInSection: section) view.contentView.addSubview(label) view.textLabel.hidden = true ...
只需添加[titleLabel setBackgroundColor:[UIColor clearColor]];
因为在我的情况下,标签重叠了绿色。
〜最佳解决schemeiPhone 3.5和4屏幕〜
从这个代码设置背景颜色或背景图像。 在这里,您不必计算截面高度,只需在xib中设置即可。
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,tableView.sectionHeaderHeight)]; sectionView.backgroundColor = [UIColor greenColor]; return sectionView; }