数据来自同一个实体,它只有不同的属性(这就是为什么我用playerFetchRequest创建了一个带参数的函数 – 以为我可以用diff参数做出不同的获取请求):
import UIKitimport CoreDataclass CustomtableVIEwCell : UItableVIEwCell { @IBOutlet var l1: UILabel? @IBOutlet var l2: UILabel? func loadItem(#number: String,name: String) { l1!.text = number l2!.text = name }}class VIEwController: UIVIEwController,UItableVIEwDelegate,NSFetchedResultsControllerDelegate,UItableVIEwDataSource { @IBOutlet var tableVIEw: UItableVIEw! //this is my second table - Ive connected it in the IB to this VC @IBOutlet var tableVIEw2: UItableVIEw! let managedobjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedobjectContext var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController() func playerFetchRequest(playerType: String) -> NSFetchRequest { let fetchRequest = NSFetchRequest(entityname: "Players") let sortDescriptor = NSSortDescriptor(key: "number",ascending: true) let filterForwards = nspredicate(format: "%K = %@","type",playerType) fetchRequest.sortDescriptors = [sortDescriptor] fetchRequest.predicate = filterForwards return fetchRequest } func getFetchedResultController(playerType: String) -> NSFetchedResultsController { fetchedResultController = NSFetchedResultsController(fetchRequest: playerFetchRequest("Forward"),managedobjectContext:managedobjectContext!,sectionnameKeyPath: nil,cachename: nil) return fetchedResultController } //remember: to create a table with multiple sections just implement the numberOfSectionsIntableVIEw(_:) method func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int { if let numberOfRowsInSection = fetchedResultController.sections?[section].numberOfObjects {return numberOfRowsInSection} else {return 0} } func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell { var cell:CustomtableVIEwCell = self.tableVIEw.dequeueReusableCellWithIDentifIEr("customCell") as CustomtableVIEwCell let player = fetchedResultController.objectAtIndexPath(indexPath) as DataModel cell.l2?.text = player.lastname + "," + player.firstname cell.l1?.text = player.number return cell } func tableVIEw(tableVIEw: UItableVIEw!,dIDdeselectRowAtIndexPath indexPath: NSIndexPath!) { tableVIEw.deselectRowAtIndexPath(indexPath,animated: true) println("You selected cell #\(indexPath.row)!") } overrIDe func vIEwDIDLoad() { var nib = UINib(nibname: "CustomtableVIEwCell",bundle: nil) tableVIEw.registerNib(nib,forCellReuseIDentifIEr: "customCell") fetchedResultController = getFetchedResultController("Forward") fetchedResultController.delegate = self fetchedResultController.performFetch(nil) super.vIEwDIDLoad() // Do any additional setup after loading the vIEw,typically from a nib. } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() // dispose of any resources that can be recreated. } func controllerDIDChangeContent(controller: NSFetchedResultsController!) { tableVIEw.reloadData() }}解决方法 我想你应该这样检查一下
- (voID)tableVIEw:(UItableVIEw *)tableVIEw dIDSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([tableVIEw isEqual: tableVIEw1]) {// Do something } else { // tableVIEw == tableVIEw2// Do something else }}
与tablevIEw的其他方法类似.
总结以上是内存溢出为你收集整理的ios,swift:多个表,单个viewcontroller全部内容,希望文章能够帮你解决ios,swift:多个表,单个viewcontroller所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)