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

前端 未结 8 2103
太阳男子
太阳男子 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: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
    }
    

提交回复
热议问题