[英]Edit static tableView cell sections
I have a static tableView with some custom cells. 我有一个静态tableView与一些自定义单元格。 What I wan't to do is change the section titles programmatically. 我不想做的是以编程方式更改节标题。 As far as I know, because the cells are static, I can't use methods like cellForRowAtIndexPath and so on, so my question is, is it possibly to change them like. 据我所知,因为单元格是静态的,我不能使用像cellForRowAtIndexPath这样的方法,所以我的问题是,它是否可能像它一样改变它们。
self.tableView.section1.text = @"title1"; // something like this?
I've tried to create an IBOutlet of the section but I get the following error: 我试图创建该部分的IBOutlet但我收到以下错误:
Unknown type name 'UITableViewSection': did you mean 'UITableViewStyle?'
What I can do is edit the content of the cells, but not the header. 我能做的是编辑单元格的内容,但不是标题。
Thanks! 谢谢!
Use viewForHeaderInSection
method. 使用viewForHeaderInSection
方法。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *label1 = [[UILabel alloc] init];
label1.frame = CGRectMake(0, 0, 190, 20);
label1.textColor = [UIColor blackColor];
// label1.font = [UIFont fontWithName:@"Helvetica Bold" size:16];
[label1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14]];
label1.textAlignment = UITextAlignmentCenter;
label1.text =[NSString stringWithFormat:@"Title %d",section];
// If your title are inside an Array then Use Below Code
label1.text =[titleArray objectAtindex:section];
label1.textColor = [UIColor whiteColor];
label1.backgroundColor = [UIColor clearColor];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[view addSubview:label1];
view.backgroundColor = [UIColor orangeColor];
return view;
}
if You want to use titleForHeaderInSection
then use below code. 如果你想使用titleForHeaderInSection
那么使用下面的代码。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"Title %d",section];
// If your title are inside an Array then Use Below Code
return [titleArray objectAtindex:section];
}
You can use the UITableViewDelegate Protocol method tableview:titleForHeaderInSection:
您可以使用UITableViewDelegate Protocol方法tableview:titleForHeaderInSection:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = @"";
switch (section) {
case 0: sectionTitle = @"Section 1"; break;
case 1: sectionTitle = @"Section 2"; break;
case 2: sectionTitle = @"Section 3"; break;
default:
break;
}
return sectionTitle;
}
Make sure you declare your <UITableViewDelegate
> in your .h file: 确保在.h文件中声明<UITableViewDelegate
>:
@interface SettingsViewController : UITableViewController <UITableViewDelegate> {
}
If you need to change the headers when the view is displayed 'live' you can do so like this: 如果您需要在视图显示为“实时”时更改标题,您可以这样做:
int sectionNumber = 0;
[self.tableView headerViewForSection:sectionNumber].textLabel.text = @"Foo Bar";
But doing so doesn't seem to change the size of the label frame. 但这样做似乎并没有改变标签框架的大小。 I just made mine larger in advance. 我提前把它做得更大。 More details here. 更多细节在这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.