gpt4 book ai didi

Swift 中的 UITableViewCell

转载 作者:IT王子 更新时间:2023-10-29 05:45:57 26 4
gpt4 key购买 nike

这是包含 UITableViewControllerCoreData 的示例代码。主文件MainTableViewController.swift:

import UIKit
import CoreData

class MainTableViewController: UITableViewController {

var results:AddrBook[]=[]

init(style: UITableViewStyle) {
super.init(style: style)
}

init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}

override func viewDidLoad() {
super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func viewDidAppear(animated: Bool) {
let request = NSFetchRequest(entityName: "Person")
request.returnsObjectsAsFaults = false
let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context:NSManagedObjectContext = appDelegate.managedObjectContext
results = context.executeFetchRequest(request, error: nil) as AddrBook[]
self.tableView.reloadData()
}

override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}

override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return results.count
}

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell

if !cell {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}

cell!.textLabel.text = results[indexPath.row].lastname + " " + results[indexPath.row].firstname

return cell
}

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject?) {

var indexPath = self.tableView.indexPathForSelectedRow()
let destViewController:DetailViewController! = segue.destinationViewController as DetailViewController

if segue.identifier == "editPerson" {
destViewController.receivedPerson = results
destViewController.indexPath = indexPath
}
}
}

如果在 cellForRowAtIndexPath 中,我使用这个:

cell!.textLabel.text = results[indexPath.row].lastname + " " + results[indexPath.row].firstname

那么一切都很好。但是如果我使用这个:

cell!.textLabel.text = results[indexPath.row].lastname
cell!.detailTextLabel.text = results[indexPath.row].firstname

我看到错误:Can't unwrap Optional.None

怎么了?请帮忙。

以防万一其他类的代码

用于添加和编辑记录的 UIViewController 类(DetailViewController.swift):

import UIKit
import CoreData

class DetailViewController: UIViewController {

@IBOutlet var currentOperation : UILabel = nil
@IBOutlet var firstnameField : UITextField = nil
@IBOutlet var lastnameField : UITextField = nil

var indexPath = NSIndexPath()
var receivedPerson:AddrBook[]=[]

init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}

override func viewDidLoad() {
super.viewDidLoad()

if !receivedPerson.isEmpty { // If selected row in tableview in MainTableViewController
currentOperation.text = "Edit Person"
firstnameField.text = receivedPerson[indexPath.row].firstname
lastnameField.text = receivedPerson[indexPath.row].lastname
}
else { // If pressed "+" in MainTableViewController
currentOperation.text = "Add Person"
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

@IBAction func saveButton(sender : AnyObject) {

let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context:NSManagedObjectContext = appDelegate.managedObjectContext

if receivedPerson.isEmpty { // If pressed "+" in MainTableViewController
let projectEntity = NSEntityDescription.entityForName("Person", inManagedObjectContext: context)
var newPerson = AddrBook(entity: projectEntity, insertIntoManagedObjectContext: context)
newPerson.lastname = lastnameField.text
newPerson.firstname = firstnameField.text
}
else { // If selected row in tableview in MainTableViewController
receivedPerson[indexPath.row].firstname = firstnameField.text
receivedPerson[indexPath.row].lastname = lastnameField.text
}

context.save(nil)
self.navigationController.popViewControllerAnimated(true)
}

}

AddrBook.swift 用于 CoreData:

import UIKit
import CoreData

@objc(AddrBook)
class AddrBook: NSManagedObject {

@NSManaged var lastname:String
@NSManaged var firstname:String
}

最佳答案

使用

cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")

代替

cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

swift 4+

使用

cell = UITableViewCell(style: .value1, reuseIdentifier: "Cell")

代替

cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")

关于Swift 中的 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24428951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com