2017-12-04 4 views
0

영어로 된 알파벳 순서로 이름이 지정된 간단한 표 셀을 사용했습니다. 나는 다른 언어로 모든 언어를 현지화 할 수 있었고 모든 것이 잘 작동합니다. 질문 : 지역화 된 이름을 사전 순으로 다른 언어로 입력하는 방법이나 명령이 있습니까?모든 현지화 가능 문자열의 알파벳 순서

import UIKit 

var animals = [“A1”, “A2”, “A3”, “A4”, “A5”]; 

class TableViewController: UITableViewController { 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return animals.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCellController; 
     cell.textLabel?.font = UIFont(name: "Futura-Bold", size: 18); 
     cell.textLabel?.textColor = UIColor.black; 
     cell.textLabel?.text = NSLocalizedString(animals[indexPath.row], comment: ""); 
     cell.smallBird.image = UIImage(named: animals[indexPath.row] + ".png"); 
     return cell 
    } 
} 

지역화 문자열

/* Animals (EN) */ 

“A1" = “Cat”; 
“A2" = “Dog”; 
“A3" = “Owl”; 
“A4" = “Leon”; 
“A5" = “Tiger”; 
+2

FYI - Swift의 줄 끝에 세미콜론이 필요하지 않습니다. – rmaddy

+0

@rmaddy하지만 OP는 문자열 지역화에 .strings 파일을 사용하고 있습니다.이 경우 세미콜론이 필요합니다. – Xcoder

+0

@Xcoder 나는 문자열 파일이 아니라 스위프트 코드에 대해 이야기하고 있습니다. – rmaddy

답변

1

당신은 순간에 키가 아닌 값으로 그들을 주문하고 있습니다.

이미지의 키와 레이블의 값이 필요하며 값으로 행을 정렬하려면 키 배열 이상이 필요합니다.

다음은 키 및 지역화 된 이름의 튜플 배열을 현지화 된 이름으로 정렬하여 만듭니다.

let animals = ["A1", "A2", "A3", "A4", "A5"].map { ($0, NSLocalizedString($0, comment: "")) }.sorted { $0.1 < $1.1 } 

그런 다음 cellForRowAt를 업데이트

cell.textLabel?.text = animals[indexPath.row].1 
cell.smallBird.image = UIImage(named: animals[indexPath.row].0) 

당신은 UIImage(named:)을 사용하는 경우 ".png를"추가 할 필요가 없습니다.