UITableView
으로 표시 될 앱의 설정에 액세스하기위한보기를 만들고 있습니다. 일부 세포는 UITableViewCellAccessoryDisclosureIndicator
, 나는 다음과 같은 종류의 UISegmentedControl와 UISwitch
하나, 다른 필요한 것 : 즉UITableView에서 다른 셀 스타일을 사용하는 방법
를, 내가 정의 세포를 필요가 있다고 생각합니다. 많은 연구를 통해, 나는 꽤 가까워졌습니다. 그러나 함께 일하기 위해 배운 모든 것을 이어 줄 수는 없습니다. 여기에 내가 무슨 짓을했는지의 :
만든 세있는 UITableViewCell 클래스 :
class DisclosureCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
class func defaultIdentifier() -> String {
return NSStringFromClass(self)
}
}
class SegmentedControlCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
class func defaultIdentifier() -> String {
return NSStringFromClass(self)
}
}
class SwitchCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
class func defaultIdentifier() -> String {
return NSStringFromClass(self)
}
}
나는 테이블 뷰 섹션 및 사용할 수있는 각 선택을 제공 할 것입니다 내 섹션 클래스와 섹션 구조체가 :
class SettingsData {
func getSectionsFromData() -> [Section] {
var settings = [Section]()
let preferences = Section(title: "OPTIONS", objects: ["Formulas", "Lifts", "Units", "Allow 15 reps"])
let patronFeatures = Section(title: "PATRON FEATURES", objects: ["Support OneRepMax"])
let feedback = Section(title: "FEEDBACK", objects: ["Send Feedback", "Please Rate OneRepMax"])
settings.append(preferences)
settings.append(patronFeatures)
settings.append(feedback)
return settings
}
}
struct Section {
var sectionHeading: String
var items: [String]
init(title: String, objects: [String]) {
sectionHeading = title
items = objects
}
}
을 마지막으로, 내 UITableViewController
:
class SettingsViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.topItem?.title = "Settings"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(self.dismissSettings(_:)))
}
func dismissSettings(sender: AnyObject?) {
self.dismissViewControllerAnimated(true, completion: nil)
}
// MARK: - Table view data source
var sections: [Section] = SettingsData().getSectionsFromData()
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].items.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section].sectionHeading
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("DisclosureCell", forIndexPath: indexPath)
cell.textLabel?.text = sections[indexPath.section].items[indexPath.row]
return cell
}
내가 도움이 필요 t이다 : 나는 각 셀을 만들 때
- , 어떻게 각 셀 내가 제대로
- I 유형에 따라뿐만 아니라 디큐 각 유형을 표시 할 수 있도록 내 세 가지 유형의 결정 않는다 각 맞춤 클래스에
func defaultIdentifier() -> String
을 만들고 각 셀 유형에 대한 스토리 보드에 재사용 식별자를 지정했습니다. 둘 다해야합니까? - 스토리 보드를 사용하고 있기 때문에 이러한 셀을 등록해야합니까, 아니면 불필요합니까?
나는이 주제에 관해 많은 스레드를 발견했으나 Objective-C에 있거나 스레드가 특정 파트에 대해 말하지 않습니다.
감사합니다.
업데이트 :
내가 업데이트 한 내 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
다음과 같이
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = tableView.cellForRowAtIndexPath(indexPath)?.reuseIdentifier
if cellIdentifier == "DisclosureCell" {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "DisclosureCell")
cell.textLabel?.text = sections[indexPath.section].items[indexPath.row]
return cell
} else if cellIdentifier == "SegmentedControlCell" {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "SegmentedControlCell")
cell.textLabel?.text = sections[indexPath.section].items[indexPath.row]
return cell
}
else {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "SwitchCell")
cell.textLabel?.text = sections[indexPath.section].items[indexPath.row]
return cell
}
}
나는 이것이 올바른 방향으로의 한 단계입니다하지만 난이 오류 때문에 여전히 뭔가를 누락 희망 :
2016년 7월 28일 07 : 15 : 35.894 한 담당자 맥스 [982 : 88,043 인한 캐치되지 않는 예외 앱 종료 'NSRangeExcept 이온 ', 이유 :' - [__ NSArrayI objectAtIndex :] : 범위를 넘는 인덱스 2 [0 .. 0] '
그리고 각 셀 아는 나는 방법을 강구해야 할 세 가지 유형 중 어느 것이 그런 다음 어떤 유형인지에 따라 위의 코드에서 설정할 수 있습니다.
업데이트 2 나는 지금 종류의 indexPath.row
를 사용하여 작업있어 :
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell", forIndexPath: indexPath) as! CustomTableViewCell
if indexPath.row == 0 {
cell.selectionStyle = .None
cell.textLabel!.text = sections[indexPath.section].items[indexPath.row]
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
} else if indexPath.row == 1 {
cell.selectionStyle = .None
cell.textLabel!.text = sections[indexPath.section].items[indexPath.row]
//cell.accessoryView = useAuthenticationSwitch
let switchView: UISwitch = UISwitch()
cell.accessoryView = switchView
switchView.setOn(false, animated: false)
return cell
} else {
cell.selectionStyle = .None
cell.textLabel!.text = sections[indexPath.section].items[indexPath.row]
let segmentedControl: UISegmentedControl = UISegmentedControl(items: ["lbs", "kg"])
segmentedControl.selectedSegmentIndex = 0
segmentedControl.addTarget(self, action: nil, forControlEvents: .ValueChanged)
cell.accessoryView = segmentedControl
return cell
}
}
문제는 indexPath.row
를 사용하여이 아닌 모든 행을 가지고있다 올바른 accessoryView
. 위의 예에서 처음 두 행은 disclosureIndicators
이고, 세 번째 행은 UISegmentedControl
이고, 네 번째 행은 UISwitch
입니다. 자, 이 왜 내 코드가 이것을 생성하는지 이해합니다. 왜냐하면 accessoryView
이 셀의 위치에 의해 결정되기 때문입니다. 대신, 나는 각 셀이 만들어 질 때이를 알릴 수 있기를 원합니다. "당신은이 설정에 대한 셀이므로, [빈칸 채우기] accessoryView
"을 얻을 수 있습니다. 각 유형에 대해 만들어진 사용자 정의 셀 클래스가 있고 그 셀 클래스를 사용하여 수행하려고 시도했지만 시도 할 수 없었습니다. 위 코드에서 하나의 사용자 정의 셀 클래스와 재사용 식별자에 갔음을 알 수 있습니다.
나는 if indexPath.row == 0 | 1 { blah, blah }
과 같은 일을 할 수 있다고 생각하지만 그 방법은 각 section
을 통해 해결되지 않을 것입니다. 이처럼 각 section
을 처리하려고하면 코드가 이미 복잡해집니다. 똑똑한 방법이 있어야합니다.
있습니까?
비슷한 질문에 대한 내 대답을보십시오. http://stackoverflow.com/questions/38565089/multiple-cutsom-cells-in-uitableview/38565553#38565553. 대답은 객관적 인 C에 있지만 그것이 당신이 찾고있는 것을 성취하는 방법에 대한 아이디어를 줄 것입니다. Swift에서 쉽게 구현할 수 있습니다. –
단일 셀이면 충분합니다. –
TableView에 대해별로 신경 쓰지 않지만 도움이 될 것 같습니다. 스토리 보드에서 tableViewController를 선택하고 속성 검사기로 이동 한 다음 원하는만큼 프로토 타입 셀의 수를 늘린 다음 요구 사항에 따라 tableView 셀을 사용자 정의하십시오. –