谁能帮我弄清楚如何向标题 View 添加图像?我尝试使用的代码不起作用。以下是我目前所拥有的。
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView*)view;
if (headerView != nil)
{
headerView.contentView.backgroundColor = [UIColor greenColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(200.0f, 0.0f, 100.0f, 25.0f)];
*imageView.image = [UIImageView = image.png];* //This line doesn't work.
[headerView.contentView addSubview:imageView];
UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 25.0f)];
mainLabel.text = [sectionTitles objectAtIndex:section];
mainLabel.backgroundColor = [UIColor redColor];
[headerView.contentView addSubview:mainLabel];
UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 26.0f, 150.0f, 20.0f)];
secondLabel.text = @"Second Label Text";
secondLabel.backgroundColor = [UIColor yellowColor];
[headerView.contentView addSubview:secondLabel];
}
}
最佳答案
请试试这个
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImage *aImage = [UIImage imageNamed:@"myHeaderImage.png"];
UIImageView *aImgView = [[UIImageView alloc] initWithImage:aImage];
aImgView.frame = CGRectMake(5, 5, 310, 60);
return aImgView;
}
关于ios - 如何将图像添加到 UITableViewHeaderFooterView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27498673/