xcode ios-tableview节标题中的图像
这是我的代码:xcode ios-tableview节标题中的图像,ios,objective-c,uitableview,tableview,Ios,Objective C,Uitableview,Tableview,这是我的代码: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIImage *myImage = [UIImage imageNamed:@"photo.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage]; imageView.frame
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImage *myImage = [UIImage imageNamed:@"photo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(10,10,60,60);
if (section == 0){
return 0;
}else if (section == 1){
return 0;
}else if (section == 2){
return 0;
}else if (section == 3){
return 0;
}else if (section == 5){
return imageView;
}else{
return 0;
}
}
因此,图像出现了,但问题是,
imageView.frame
不起作用,照片大小似乎等于页眉高度,如何解决此问题?在实现tableView:viewForHeaderInSection:
方法时,还必须实现tableView:heightForHeaderInSection:
方法以返回所需的收割台高度:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 5) {
return 70;
} else {
return 0;
}
您的viewForHeaderInSection
方法可以做得更好:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 5) {
UIImage *myImage = [UIImage imageNamed:@"photo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(10, 10, 60, 60);
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
[view addSubview:imageView];
return view;
} else {
return nil;
}
}
在执行
tableView:viewForHeaderInSection:
方法时,还必须执行tableView:heightForHeaderInSection:
方法以返回所需的收割台高度:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 5) {
return 70;
} else {
return 0;
}
您的viewForHeaderInSection
方法可以做得更好:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 5) {
UIImage *myImage = [UIImage imageNamed:@"photo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(10, 10, 60, 60);
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
[view addSubview:imageView];
return view;
} else {
return nil;
}
}
在执行
tableView:viewForHeaderInSection:
方法时,还必须执行tableView:heightForHeaderInSection:
方法以返回所需的收割台高度:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 5) {
return 70;
} else {
return 0;
}
您的viewForHeaderInSection
方法可以做得更好:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 5) {
UIImage *myImage = [UIImage imageNamed:@"photo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(10, 10, 60, 60);
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
[view addSubview:imageView];
return view;
} else {
return nil;
}
}
在执行
tableView:viewForHeaderInSection:
方法时,还必须执行tableView:heightForHeaderInSection:
方法以返回所需的收割台高度:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 5) {
return 70;
} else {
return 0;
}
您的viewForHeaderInSection
方法可以做得更好:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 5) {
UIImage *myImage = [UIImage imageNamed:@"photo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(10, 10, 60, 60);
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
[view addSubview:imageView];
return view;
} else {
return nil;
}
}
谢谢,很好用。我可以在图像后面添加背景吗?你可以在视图中添加任何你想添加的内容。谢谢,效果很好。我可以在图像后面添加背景吗?你可以在视图中添加任何你想添加的内容。谢谢,效果很好。我可以在图像后面添加背景吗?你可以在视图中添加任何你想添加的内容。谢谢,效果很好。我可以在图像后面添加背景吗?你可以在视图中添加任何你想要的内容。