programing

UITableView Section 제목을 프로그래밍 방식으로 설정하는 방법(iPhone/iPad)은 무엇입니까?

javajsp 2023. 6. 3. 08:13

UITableView Section 제목을 프로그래밍 방식으로 설정하는 방법(iPhone/iPad)은 무엇입니까?

다음을 만들었습니다.UITableView인터페이스 작성기에서 사용storyboards.그UITableView으로 설정됩니다.static cells다양한 섹션이 있습니다.

제가 안고 있는 문제는 앱을 여러 언어로 설정하려고 한다는 것입니다.이를 위해 저는 변경할 수 있어야 합니다.UITableView어떻게든 섹션 제목.

누가 저를 좀 도와주실 수 있나요?이상적으로 저는 이 문제에 접근하고 싶습니다.IBOutlets하지만 저는 이것이 이 경우에도 가능하지 않다고 생각합니다.어떤 조언과 제안이든 정말 감사하겠습니다.

UI 테이블 보기를 연결한 후delegate그리고.datasource컨트롤러에서 다음과 같은 작업을 수행할 수 있습니다.

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

스위프트

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
}

Swift에서 코드를 작성하는 경우 다음과 같은 예를 들 수 있습니다.

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    switch section
    {
        case 0:
            return "Apple Devices"
        case 1:
            return "Samsung Devices"
        default:
            return "Other Devices"
    }
}

UITableViewDataSource 메서드 사용

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

titleForHeaderInSection은 다음과 같이 섹션 쓰기의 헤더 텍스트를 적용하기 위해 UITableView의 위임 방법입니다.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"Hello World";
}

참고:-(NSString *)tableView: titleForHeaderInSection:다음의 경우 UITableView에서 호출되지 않습니다.- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectionUITableView의 대리인으로 구현됩니다.

의 과거 버전에 대해서는 잘 모르겠습니다.UITableView프로토콜, 하지만 iOS 9부터는func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?의 일부입니다.UITableViewDataSource의전

   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"
      }
   }

다음을 선언할 필요가 없습니다.delegate데이터로 테이블을 채웁니다.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return 45.0f; 
//set height according to row or section , whatever you want to do!
}

횡단 레이블 텍스트가 설정되었습니다.

- (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;
}

다른 답변에는 문제가 없지만 이 답변은 작은 정적 테이블이 있는 상황에서 유용할 수 있는 비프로그래밍 솔루션을 제공합니다.장점은 스토리보드를 사용하여 현지화를 구성할 수 있다는 것입니다.XLIFF 파일을 통해 Xcode에서 현지화를 계속 내보낼 수 있습니다.또한 Xcode 9에는 현지화를 쉽게 하기 위한가지 새로운 도구가 있습니다.

(원본)

저도 비슷한 요구사항이 있었습니다.메인 스토리보드(베이스)에 정적 셀이 있는 정적 테이블이 있습니다.예를 들어 .string 파일을 사용하여 섹션 제목을 현지화합니다.Maint.strings(독일어)는 스토리보드에서 섹션을 선택하고 객체 ID를 기록합니다.

개체 ID

그런 다음 문자열 파일(Main.strings(독일어)로 이동하여 다음과 같이 번역을 삽입합니다.

"MLo-jM-tSN.headerTitle" = "Localized section title";

추가 리소스:

언급URL : https://stackoverflow.com/questions/10505708/how-to-set-the-uitableview-section-title-programmatically-iphone-ipad