ios swift – 表视图与多个数据源/笔尖

我想创build一个表视图,从两个不同的数据源加载它的数据,并根据它是哪一个select一个特定的笔尖。 如何创build一个混合来自两个对象数组的元素的提要?

我正在考虑创build一个对象数组,按datesorting对象。 然后,我会检查每个对象的types是在我的cellforrowatindexpath方法,并使用相应的笔尖。

这是做这个最有效的方法吗?

谢谢

cellForRowAtIndexPath创build单个对象数组并正确处理它们是一种好方法。

你应该使用表视图注册你的每一个nib,可能是使用registerNibForCellReuseIdentifierviewDidLoadregisterNibForCellReuseIdentifier 。 确保每个使用不同的reuseIdentifier ,然后在cellForRowAtIndexPath中将正确的types出列,一旦确定了它应该是哪种types的单元格。

 override func viewDidLoad() { super.viewDidLoad() tableView.registerNib(UINib(nibName: "fooCell", bundle: .mainBundle()), forCellReuseIdentifier: "fooCellIdentifier"); tableView.registerNib(UINib(nibName: "barCell", bundle: .mainBundle()), forCellReuseIdentifier: "barCellIdentifier"); } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell: UITableViewCell? if (indexPathIsFooCell(indexPath)) { cell = tableView.dequeueReusableCellWithIdentifier("fooCellIdentifier", forIndexPath: indexPath) } else { cell = tableView.dequeueReusableCellWithIdentifier("barCellIdentifier", forIndexPath: indexPath) } //customise the cell return cell! } 

这是另一个可能帮助你的解决scheme

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.section == 0 { var cell = tableView.dequeueReusableCellWithIdentifier("relatedTableViewCell") as? relatedTableViewCell if cell == nil { let nib:Array = NSBundle.mainBundle().loadNibNamed("relatedTableViewCell", owner: self, options: nil) cell = nib[0] as? relatedTableViewCell } let info = relatedQuizArr.objectAtIndex(indexPath.row) as! relatedInfo cell?.relatedImage.sd_setImageWithURL(NSURL(string: info.rqImage!)) cell?.relatedLabel.text = (String: info.rqName!) cell?.rQuizView.layer.shadowOffset = CGSizeMake(-1, 1) cell?.rQuizView.layer.shadowOpacity = 0.4 return cell! } else { var cell = tableView.dequeueReusableCellWithIdentifier("relatedTableViewCell") as? relatedTableViewCell if cell == nil { let nib:Array = NSBundle.mainBundle().loadNibNamed("relatedTableViewCell", owner: self, options: nil) cell = nib[0] as? relatedTableViewCell } if indexPath.row == 0 { let label = UILabel(frame: CGRectMake(22, 10, 200, 40)) label.font = label.font.fontWithSize(25) label.textAlignment = NSTextAlignment.Center label.text = "Featured Quiz" cell!.addSubview(label) } else { } let info = featuredQuizArray.objectAtIndex(indexPath.row) as! relatedInfo cell?.relatedImage.sd_setImageWithURL(NSURL(string: info.featuredqImage!)) cell?.relatedLabel.text = (String: info.featuredqName!) cell?.rQuizView.layer.shadowOffset = CGSizeMake(-1, 1) cell?.rQuizView.layer.shadowOpacity = 0.4 return cell! } 

希望它会有所帮助。