Shi*_*aji 97 iphone ios swift xcode6
我对编码很新,对Xcode(Swift)来说真的很新.我知道我需要注册一个笔尖或一个班级,但我不明白'在哪里或如何?'.
import UIKit
class NotesListViewController: UITableViewController {
@IBOutlet weak var menuButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "preferredContentSizeChanged:",
name: UIContentSizeCategoryDidChangeNotification,
object: nil)
// Side Menu
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// whenever this view controller appears, reload the table. This allows it to reflect any changes
// made whilst editing notes
tableView.reloadData()
}
func preferredContentSizeChanged(notification: NSNotification) {
tableView.reloadData()
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notes.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let note = notes[indexPath.row]
let font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
let textColor = UIColor(red: 0.175, green: 0.458, blue: 0.831, alpha: 1)
let attributes = [
NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSTextEffectAttributeName : NSTextEffectLetterpressStyle
]
let attributedString = NSAttributedString(string: note.title, attributes: attributes)
cell.textLabel?.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.textLabel?.attributedText = attributedString
return cell
}
let label: UILabel = {
let temporaryLabel = UILabel(frame: CGRect(x: 0, y: 0, width: Int.max, height: Int.max))
temporaryLabel.text = "test"
return temporaryLabel
}()
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
label.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
label.sizeToFit()
return label.frame.height * 1.7
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
notes.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
// #pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let editorVC = segue.destinationViewController as? NoteEditorViewController {
if "CellSelected" == segue.identifier {
if let path = tableView.indexPathForSelectedRow() {
editorVC.note = notes[path.row]
}
} else if "AddNewNote" == segue.identifier {
let note = Note(text: " ")
editorVC.note = note
notes.append(note)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Aks*_*Aks 94
您可以UITableViewCell为此注册一个类:
使用Swift 3+:
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
Run Code Online (Sandbox Code Playgroud)
使用Swift 2.2:
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
Run Code Online (Sandbox Code Playgroud)
确保同样的标识符" cell"也在您的故事板中复制UITableViewCell.
" self"是让类使用后跟的类名.self.
Jay*_*kar 30
这对我有用,也可以帮助你:
Swift 4+:
self.tableView.register(UITableViewCell.self, forCellWithReuseIdentifier: "cell")
Run Code Online (Sandbox Code Playgroud)
斯威夫特3:
self.tableView.register(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)
Swift 2.2:
self.tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)
我们必须将Identifier属性设置为Table View Cell,如下图所示,
xhi*_*oda 18
我的情况我通过在Table View Cell的"Identifier"属性中命名它来解决这个问题:
别忘了:在你的Class中声明:UITableViewDataSource
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
Run Code Online (Sandbox Code Playgroud)
Roh*_*ngh 14
当 Swift 文件和 Storyboard 中 Tablecell 的标识符名称不同时,会发生此错误。
例如,在我的例子中,标识符是placecellIdentifier。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "placecellIdentifier", for: indexPath)
// Your code
return cell
}
Run Code Online (Sandbox Code Playgroud)
LC *_*C 웃 10
只需拖动一个单元格(就像你为TableViewController所做的那样)并通过释放TableViewController上的单元格来添加它.单击单元格并转到其属性检查器并将其标识符设置为"单元格".希望它可以工作.
不要忘记在Attributes Inspector上想要标识符.
(不是 "身份检查员"中的"恢复身份证"!)
小智 7
发生此问题的另一个原因是较早的问题。当显示一个新的ViewController时,直接实例化目标ViewController当然不会从StoryBoard加载原型单元。正确的解决方案应该始终是通过故事板实例化视图控制器,如下所示:
storyboard?.instantiateViewController(withIdentifier: "some_identifier")
Run Code Online (Sandbox Code Playgroud)
小智 6
在 Swift 3.0 中,为您的 UITableViewCell 注册一个类,如下所示:
tableView.register(UINib(nibName: "YourCellXibName", bundle: nil), forCellReuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
97254 次 |
| 最近记录: |