简体   繁体   English

Swift 2.0在一个视图控制器cellForRowIndexPath中包含两个表视图

[英]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.

相关问题 视图控制器中的两个表视图:Swift - Two table views in view controller: Swift 快速加载两个集合视图和一个表视图的coredata - Loading coredata for two collection views and one table view in swift Swift 4 One View Controller两个视图隐藏导航栏 - Swift 4 One View Controller two views hide navigation bar 不同视图控制器中的两个表视图一个工作一个不工作(Swift 4) - Two Table Views In Different View Controllers one working and one not(Swift 4) 在集合视图控制器中快速移动两个视图 - Swift a two views within a collection view controller 无法在一个视图控制器中加载两个表视图 - unable to load two table views in one view controller 如何使用两个自定义 UITableViewCell 在一个视图控制器中创建两个表视图? - How do I create two table views in one view controller with two custom UITableViewCells? 您可以在一个视图控制器上使用两个不同的相机按钮来显示两个不同的图像吗? (快速3) - Can you have two different camera buttons for two different image views on one view controller? (Swift 3) 如何通过根表视图控制器中的swift在两个表视图之间建立Segue连接? - How to establish Segue connection between two table views via swift in the root table view controller? 快速地将具有分层表视图的导航控制器放入视图控制器中 - Putting a navigation controller with hierarchical table views inside of a view controller in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM