[英]Swift 2.0 two table views in one view controller cellForRowIndexPath
Ive seen a few questions here about this but no defitnive answer Im using an up to date version of Xcode and swift... 我在这里看到了一些与此有关的问题,但没有合适的答案。我正在使用最新版本的Xcode和Swift ...
Im trying to work with two table views in one view controller here is my cellForRowIndexPath function 我试图在一个视图控制器中使用两个表视图,这是我的cellForRowIndexPath函数
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell!
if(tableView == self.allTableView){
cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BMRadioAllTableViewCell
cell.mixImage.image = mixPhotoArray[indexPath.item]
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
cell.mixDateLabel.text = dateFormatter.stringFromDate(mixDateArray[indexPath.item])
}
if(tableView == self.featuredTableView){
// SET UP THE NEXT TABLE VIEW
}
return cell
}
Im getting the error "Value of type UITableViewCell has no member "xxxx" so its obviously not registering the change to cell I'm making in the if statement. 我收到错误消息“ UITableViewCell类型的值没有成员“ xxxx”,因此它显然未在if语句中注册对单元格所做的更改。
I have tried various other ways, like declaring the variable within the if statement and returning it there. 我尝试了各种其他方式,例如在if语句中声明变量并将其返回。 But you get the error "Missing return in a function expected to return UITableViewCell" since you need to get it outside the if statement. 但是,由于需要在if语句之外获取它,因此会收到错误“在期望返回UITableViewCell的函数中缺少返回”。
If you have two different cell types for two different tables, you should make your code look something like this: 如果两个不同的表具有两种不同的单元格类型,则应使代码看起来像这样:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(tableView == self.allTableView){
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BMRadioAllTableViewCell
cell.mixImage.image = mixPhotoArray[indexPath.item]
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
cell.mixDateLabel.text = dateFormatter.stringFromDate(mixDateArray[indexPath.item])
return cell;
} else {
var cell = ...
// SET UP THE NEXT TABLE VIEW
return cell
}
}
There's no need to have a single generic cell
variable that handles both tables. 无需具有处理两个表的单个通用cell
变量。
The error doesn't really have to do with you trying to configure two table views. 该错误实际上与您尝试配置两个表视图无关。
Even though you're casting the result of dequeueReusableCellWithIdentifier:forIndexPath:
to BMRadioAllTableViewCell
, you are then assigning it to a variable of type UITableViewCell!
即使将dequeueReusableCellWithIdentifier:forIndexPath:
的结果强制转换为BMRadioAllTableViewCell
,您BMRadioAllTableViewCell
其分配给UITableViewCell!
类型的变量UITableViewCell!
. 。 As such, it will be a compiler error for you to access fields of BMRadioAllTableViewCell
. 这样,您访问BMRadioAllTableViewCell
字段将是编译器错误。
You would either need to change the cell
type to BMRadioAllTableViewCell
, or have a locally scoped variable of the correct type that you configure and then assign to cell
: 您可能需要将cell
类型更改为BMRadioAllTableViewCell
,或者需要具有您配置的正确类型的本地范围内的变量,然后将其分配给cell
:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell!
if(tableView == self.allTableView){
let bmRadioAllCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BMRadioAllTableViewCell
bmRadioAllCell.mixImage.image = mixPhotoArray[indexPath.item]
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
bmRadioAllCell.mixDateLabel.text = dateFormatter.stringFromDate(mixDateArray[indexPath.item])
cell = bmRadioAllCell
}
if(tableView == self.featuredTableView){
// SET UP THE NEXT TABLE VIEW
}
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.