代码如下:
import UIKit
class SliderMenuTableViewController: UITableViewController {
var tableArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
let age: String
age = String(LoginViewController.AGE)
let Profile = LoginViewController.NAME! + ", " + age
tableArray = [Profile,"Second","Third","Fourth","Logout"]
}
// table view delegate method
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(tableArray[indexPath.row], forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = tableArray[indexPath.row]
if cell.textLabel?.text == tableArray[0] {
cell.imageView?.frame = CGRectMake(10, 10, 50, 50)
cell.imageView!.layer.borderWidth = 1
cell.imageView!.layer.masksToBounds = false
cell.imageView!.layer.borderColor = UIColor.blackColor().CGColor
cell.imageView!.layer.cornerRadius = cell.imageView!.frame.width/2
cell.imageView!.clipsToBounds = true
cell.imageView?.image = LoginViewController.AVATAR
}
return cell
}
发生错误:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使具有标识符 Viktor 的单元格出队,可选 (29) - 必须为标识符注册一个 nib 或一个类,或者连接一个原型(prototype)单元格 Storyboard
我知道我必须识别单元格,如何从代码中执行此操作会导致我的 tableArray
中的变量“Profile”不是静态的,我无法从 Storyboard 中识别它?
最佳答案
您没有在单元格中提供正确的单元格标识符,因为它正在崩溃。您需要在此处提供的 CellIdentifier 方法将用于单元格。将同一单元格的相同标识符放在 Storyboard中。
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell
//configure your cell
return cell
}
根据您使用自定义单元格的方式,您必须通过以下方式注册单元格
注册自定义单元格
tableView.registerClass(MyCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)
向 Xib 注册自定义单元格:
tableView.registerNib(UINib(nibName: "UICustomTableViewCell", bundle: nil), forCellReuseIdentifier: "UICustomTableViewCell")
更新:如果您通过 XIB 创建单元格,您也可以在 XIB 中分配单元格标识符。
关于swift - 如何在 TableView 中使用单元格标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39304744/