[英]UITableView Background Image for Static Section/Cells
我知道我們可以在動態表格視圖/單元格的節標題中添加背景圖像/顏色,但是有人可以使用靜態單元格在表格視圖中幫我做同樣的事情嗎?
我想做的是在使用靜態單元格的TableView的第3部分中使用背景圖像(共有4個部分)
我想添加背景圖像並更改文本顏色以為第三部分說一些RGB值
您可以使用UITableView的委托方法來設置節標題的高度和視圖。 這應該做您想要的:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return (section == 2)? 100:30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
label.text = @"Description";
label.textColor = [UIColor whiteColor];
[imageView addSubview:label];
imageView.image = [UIImage imageNamed:@"House.tiff"];
return (section == 2)? imageView:nil;
}
UITableViewCells
具有backgroundView
屬性,該屬性是UIView
。 例如,您可以將其更改為UIImageView
,或為表視圖單元格構建更復雜的背景。 單元格是靜態還是動態都沒有關系。
然后,可以通過設置UITableViewCells
cell.textLabel.textColor
來簡單地更改單元格的文本顏色。
同樣適用於UITableViewHeaderFooterView
,它(如名稱所示)用於表視圖節的頁眉和頁腳。 在代碼中,您可以使用以下命令訪問節的標題
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section
這樣可以避免您完全從頭重新創建標頭,而只需進行一些小的調整即可。 或者,覆蓋
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
並構建一個自定義UIView
,它將成為您的標題。
您應為中的靜態單元格分配一個標記(例如15)
tableview:didSelectRowForIndexPath
接着
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(cell.tag==15)
cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:250/255.0 blue:243/255.0 alpha:1.0];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.