ios - example - xcode swift viewcontroller
Swift의 펜촉에서 사용자 정의 UITableViewCell (6)
펜촉에서 사용자 정의 테이블 뷰 셀을 만들려고합니다. 이 기사를 here 참조하고 here . 두 가지 문제에 직면하고 있습니다.
UITableViewCell 객체를 드래그하여 .xib 파일을 만들었습니다.
UITableViewCell
의 하위 클래스를 만들어 셀의 클래스로,
Cell
을 재사용 가능한 식별자로 설정했습니다.
import UIKit
class CustomOneCell: UITableViewCell {
@IBOutlet weak var middleLabel: UILabel!
@IBOutlet weak var leftLabel: UILabel!
@IBOutlet weak var rightLabel: UILabel!
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
UITableViewController에는이 코드가 있습니다.
import UIKit
class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
var items = ["Item 1", "Item2", "Item3", "Item4"]
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - UITableViewDataSource
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let identifier = "Cell"
var cell: CustomOneCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomOneCell
if cell == nil {
tableView.registerNib(UINib(nibName: "CustomCellOne", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomOneCell
}
return cell
}
}
이 코드는 오류가 없지만 시뮬레이터에서 실행할 때 다음과 같습니다.
스토리 보드의 UITableViewController에서 나는 셀에 아무것도하지 않았습니다. 빈 식별자이며 하위 클래스가 없습니다. 프로토 타입 셀에 셀 식별자를 추가하고 다시 실행했지만 동일한 결과를 얻습니다.
내가 직면 한 또 다른 오류는 UITableViewController에서 다음 메소드를 구현하려고 할 때입니다.
override func tableView(tableView: UITableView!, willDisplayCell cell: CustomOneCell!, forRowAtIndexPath indexPath: NSIndexPath!) {
cell.middleLabel.text = items[indexPath.row]
cell.leftLabel.text = items[indexPath.row]
cell.rightLabel.text = items[indexPath.row]
}
내가 언급 한 기사에서 볼 수 있듯이
cell
매개 변수의 유형 양식
UITableViewCell
을 UITableViewCell의 하위 클래스 인
CustomOneCell
변경했습니다.
하지만 다음과 같은 오류가 발생합니다.
선택기 'tableView : willDisplayCell : forRowAtIndexPath :'를 사용하여 메서드를 재정의하는 경우 '(UITableView !, CustomOneCell !, NSIndexPath!)-> ()'형식이 호환되지 않습니다.
누구나 이러한 오류를 해결하는 방법을 알고 있습니까? 이것들은 Objective-C에서 잘 작동하는 것 같습니다.
감사합니다.
편집 : 방금 시뮬레이터의 방향을 가로로 변경하고 세로로 다시 돌리면 셀이 나타납니다! 나는 아직도 무슨 일이 일어나고 있는지 알 수 없었다. here Xcode 프로젝트를 업로드하여 빠르게 살펴볼 시간이 있으면 문제를 보여줍니다.
Swift 5 및 iOS 12.2에서는 문제를 해결하기 위해 다음 코드를 시도해야합니다.
CustomCell.swift
import UIKit
class CustomCell: UITableViewCell {
// Link those IBOutlets with the UILabels in your .XIB file
@IBOutlet weak var middleLabel: UILabel!
@IBOutlet weak var leftLabel: UILabel!
@IBOutlet weak var rightLabel: UILabel!
}
TableViewController.swift
import UIKit
class TableViewController: UITableViewController {
let items = ["Item 1", "Item2", "Item3", "Item4"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
}
// MARK: - UITableViewDataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.middleLabel.text = items[indexPath.row]
cell.leftLabel.text = items[indexPath.row]
cell.rightLabel.text = items[indexPath.row]
return cell
}
}
아래 이미지는 Xcode의 제한 조건 모호성 메시지없이 제공된 코드에서 작동하는 제한 조건 세트를 보여줍니다.
다음과 같이 펜촉을 등록하지 않았습니다 :
tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
당신에게 도움이 될 수있는 또 다른 방법은 내가 등록하는 것입니다.
다음과 같이 사용자 정의 tableView를 작성한다고 가정하십시오.
class UICustomTableViewCell: UITableViewCell {...}
그런 다음 "registerClass"를 사용하여 표시 할 UITableViewController에이 셀을 등록 할 수 있습니다.
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(UICustomTableViewCell.self, forCellReuseIdentifier: "UICustomTableViewCellIdentifier")
}
셀의 행 메소드에서 예상 한대로 호출 할 수 있습니다.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UICustomTableViewCellIdentifier", forIndexPath: indexPath) as! UICustomTableViewCell
return cell
}
스위프트 4
펜촉 등록
tblMissions.register(UINib(nibName: "MissionCell", bundle: nil), forCellReuseIdentifier: "MissionCell")
TableView 데이터 소스에서
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MissionCell", for: indexPath) as? MissionCell else { return UITableViewCell() }
return cell
}
"Overrideing method ... has not compatible type ..." 오류 수정을 위해 함수 선언을 다음과 같이 변경했습니다.
override func tableView(tableView: (UITableView!),
cellForRowAtIndexPath indexPath: (NSIndexPath!))
-> UITableViewCell {...}
(
-> UITableViewCell!
-끝에 느낌표가 있음)
UITableViewCell 클래스를 사용하여 xib를 간단하게 가져옵니다. UI를 reuirement에 따라 설정하고 IBOutlet을 지정하십시오. 다음과 같이 테이블 뷰의 cellForRowAt ()에서 사용하십시오.
//MARK: - table method
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arrayFruit.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:simpleTableViewCell? = tableView.dequeueReusableCell(withIdentifier:"simpleTableViewCell") as? simpleTableViewCell
if cell == nil{
tableView.register(UINib.init(nibName: "simpleTableViewCell", bundle: nil), forCellReuseIdentifier: "simpleTableViewCell")
let arrNib:Array = Bundle.main.loadNibNamed("simpleTableViewCell",owner: self, options: nil)!
cell = arrNib.first as? simpleTableViewCell
}
cell?.labelName.text = self.arrayFruit[indexPath.row]
cell?.imageViewFruit.image = UIImage (named: "fruit_img")
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100.0
}
아무런 문제없이 100 % 일하고 (테스트 됨)