一个视图控制器cellForRowIndexPath中的Swift 2.0两个表视图

时间:2015-11-18 23:17:22

标签: ios swift uitableview

我已经在这里看到了一些关于这个的问题,但没有任何明确的答案我使用最新版本的Xcode和swift ......

我试图在一个视图控制器中使用两个表视图,这是我的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
    }

我得到错误“UITableViewCell类型的值没有成员”xxxx“因此它显然没有将更改注册到我在if语句中创建的单元格。

我尝试了其他各种方法,比如在if语句中声明变量并将其返回到那里。但是你得到错误“在函数中缺少返回UITableViewCell”,因为你需要在if语句之外得到它。

2 个答案:

答案 0 :(得分:2)

如果两个不同的表有两种不同的单元格类型,则应使代码看起来像这样:

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
    }
}

没有必要使用一个处理两个表的通用cell变量。

答案 1 :(得分:0)

错误与您尝试配置两个表视图无关。

即使您将dequeueReusableCellWithIdentifier:forIndexPath:的结果转换为BMRadioAllTableViewCell,您也可以将其分配给UITableViewCell!类型的变量。因此,您访问BMRadioAllTableViewCell字段时会出现编译错误。

您需要将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
}
相关问题