【问题标题】:Edit static tableView cell sections编辑静态 tableView 单元格部分
【发布时间】:2012-12-22 08:16:07
【问题描述】:

我有一个带有一些自定义单元格的静态 tableView。我不想做的是以编程方式更改章节标题。据我所知,因为单元格是静态的,所以我不能使用 cellForRowAtIndexPath 之类的方法,所以我的问题是,是否可以更改它们。

self.tableView.section1.text = @"title1"; // something like this?

我尝试创建该部分的 IBOutlet,但出现以下错误:

Unknown type name 'UITableViewSection': did you mean 'UITableViewStyle?'

我能做的是编辑单元格的内容,而不是标题。

谢谢!

【问题讨论】:

    标签: ios xcode static tableview cell


    【解决方案1】:

    使用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;
    
        }
    

    如果您想使用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];
    }
    

    【讨论】:

      【解决方案2】:

      可以使用 UITableViewDelegate 协议方法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;
      }
      

      确保在 .h 文件中声明您的 <UITableViewDelegate>:

      @interface SettingsViewController : UITableViewController <UITableViewDelegate> {
      
      }
      

      【讨论】:

      • 只要我在视图加载之前设置了部分标题,这就会很好地工作。但是,我希望更改表格标题以响应表格上的用户操作。有什么方法可以强制重新加载表或反映这些更改(假设我将 sectionTitle 设置为更改的本地属性值)?
      • 想通了:这样做更容易:[self.tableView headerViewForSection:1].textLabel.text = @"blah"; Via stackoverflow.com/a/17959411/1304462
      【解决方案3】:

      如果您需要在视图“实时”显示时更改标题,您可以这样做:

      int sectionNumber = 0;
      [self.tableView headerViewForSection:sectionNumber].textLabel.text = @"Foo Bar";
      

      但这样做似乎并没有改变标签框的大小。我只是提前把我的放大了。 More details here.

      【讨论】:

        猜你喜欢
        • 2015-10-02
        • 1970-01-01
        • 2014-02-22
        • 2015-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-29
        • 2017-06-30
        相关资源
        最近更新 更多