How to set the UITableView Section title programmatically (iPhone/iPad)?

前端 未结 8 2095
太阳男子
太阳男子 2020-12-07 10:54

I\'ve created a UITableView in Interface Builder using storyboards. The UITableView is setup with static cells and a numb

相关标签:
8条回答
  • 2020-12-07 11:33

    If you are writing code in Swift it would look as an example like this

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
    {
        switch section
        {
            case 0:
                return "Apple Devices"
            case 1:
                return "Samsung Devices"
            default:
                return "Other Devices"
        }
    }
    
    0 讨论(0)
  • 2020-12-07 11:33

    titleForHeaderInSection is a delegate method of UITableView so to apply header text of section write as follows,

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
                  return @"Hello World";
    }
    
    0 讨论(0)
  • 2020-12-07 11:34

    I don't know about past versions of UITableView protocols, but as of iOS 9, func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? is part of the UITableViewDataSource protocol.

       class ViewController: UIViewController {
    
          @IBOutlet weak var tableView: UITableView!
    
          override func viewDidLoad() {
             super.viewDidLoad()
             tableView.dataSource = self
          }
       }
    
       extension ViewController: UITableViewDataSource {
          func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
             return "Section name"
          }
       }
    

    You don't need to declare the delegate to fill your table with data.

    0 讨论(0)
  • 2020-12-07 11:42
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
       return 45.0f; 
    //set height according to row or section , whatever you want to do!
    }
    

    section label text are set.

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *sectionHeaderView;
    
            sectionHeaderView = [[UIView alloc] initWithFrame:
                                 CGRectMake(0, 0, tableView.frame.size.width, 120.0)];
    
    
        sectionHeaderView.backgroundColor = kColor(61, 201, 247);
    
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:
                                CGRectMake(sectionHeaderView.frame.origin.x,sectionHeaderView.frame.origin.y - 44, sectionHeaderView.frame.size.width, sectionHeaderView.frame.size.height)];
    
        headerLabel.backgroundColor = [UIColor clearColor];
        [headerLabel setTextColor:kColor(255, 255, 255)];
        headerLabel.textAlignment = NSTextAlignmentCenter;
        [headerLabel setFont:kFont(20)];
        [sectionHeaderView addSubview:headerLabel];
    
        switch (section) {
            case 0:
                headerLabel.text = @"Section 1";
                return sectionHeaderView;
                break;
            case 1:
                headerLabel.text = @"Section 2";
                return sectionHeaderView;
                break;
            case 2:
                headerLabel.text = @"Section 3";
                return sectionHeaderView;
                break;
            default:
                break;
        }
    
        return sectionHeaderView;
    }
    
    0 讨论(0)
  • 2020-12-07 11:46

    Once you have connected your UITableView delegate and datasource to your controller, you could do something like this:

    ObjC

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
        NSString *sectionName;
        switch (section) {
            case 0:
                sectionName = NSLocalizedString(@"mySectionName", @"mySectionName");
                break;
            case 1:
                sectionName = NSLocalizedString(@"myOtherSectionName", @"myOtherSectionName");
                break;
            // ...
            default:
                sectionName = @"";
                break;
        }    
        return sectionName;
    }
    

    Swift

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    
        let sectionName: String
        switch section {
            case 0:
                sectionName = NSLocalizedString("mySectionName", comment: "mySectionName")
            case 1:
                sectionName = NSLocalizedString("myOtherSectionName", comment: "myOtherSectionName")
            // ...
            default:
                sectionName = ""
        }
        return sectionName
    }
    
    0 讨论(0)
  • 2020-12-07 11:48

    Use the UITableViewDataSource method

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    
    0 讨论(0)
提交回复
热议问题