프로그래밍 방식으로 UITableView 섹션 제목을 설정하는 방법 (iPhone / iPad)?
을 UITableView
사용하여 인터페이스 빌더를 만들었습니다 storyboards
. 은 ( UITableView
는) static cells
다양한 섹션으로 설정되어 있습니다.
내가 겪고있는 문제는 여러 언어로 앱을 설정하려고한다는 것입니다. 이렇게하려면 UITableView
어떻게 든 섹션 제목 을 변경할 수 있어야합니다 .
누군가 나를 도울 수 있습니까? 이상적으로는 사용하여 문제에 접근하고 싶지만 IBOutlets
이 경우에는 이것이 가능하지 않다고 생각합니다. 어떤 조언과 제안이라도 정말 감사하겠습니다.
미리 감사드립니다.
UITableView 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:
경우 jQuery과에 의해 호출되지 않습니다 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
jQuery과의 위임에 구현;
- (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에는 현지화를 보다 쉽게 해주는 몇 가지 새로운 도구가 있습니다.
(실물)
I had a similar requirement. I had a static table with static cells in my Main.storyboard(Base). To localize section titles using .string files e.g. Main.strings(German) just select the section in storyboard and note the Object ID
Afterwards go to your string file, in my case Main.strings(German) and insert the translation like:
"MLo-jM-tSN.headerTitle" = "Localized section title";
Additional Resources:
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.
'Programming' 카테고리의 다른 글
전체 라인을 sed로 바꾸는 방법? (0) | 2020.08.10 |
---|---|
디렉토리에 파일이 포함되어 있는지 쉘 스크립트에서 확인 (0) | 2020.08.10 |
세션 상태 서버에 세션 상태 요청을 할 수 없습니다. (0) | 2020.08.10 |
개인 생성자에 테스트 커버리지를 추가하는 방법은 무엇입니까?PHP : stdClass 객체 계산 (0) | 2020.08.10 |
자바 스크립트 개체의 첫 번째 키 이름 가져 오기 (0) | 2020.08.10 |