ios - TableView 不返回数据

标签 ios swift uitableview uicollectionview

我是一名学生 iOS 开发人员,我正在尝试控制返回 3 个(或更多) TableView 的 Collection View 单元格中的 TableView ,以便我可以拥有多个 TableView 。我相信我正确地实现了一切,但是没有数据返回到各个 TableView 我已经在 TableView 的原型(prototype)单元格中设置了重用标识符,并且委托(delegate)和数据源也设置为 VC。

var tableView1: UITableView?
var tableview2: UITableView?


    // MARK: - Table view data source

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    if tableView == tableView1 {

        return 2;

    } else if tableView == tableview2 {

        return 3
    }
    return 0;

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    if tableView == tableView1 {
        return 2;

    } else if tableView == tableview2 {

        return 1;
    }
    return 0;

}



func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    if tableView == tableView1 {
        cell = tableView.dequeueReusableCellWithIdentifier("testcell1", forIndexPath: indexPath) as! UITableViewCell

    } else if tableView == tableview2 {

        cell = tableView.dequeueReusableCellWithIdentifier("testcell2", forIndexPath: indexPath) as! UITableViewCell
    }

    // Configure the cell...
    if tableView == tableView1 {

    cell.textLabel?.text = "Homeroom"
    cell.detailTextLabel?.text = "8:15 AM - 9:00 AM"
    cell.selectionStyle = .None

    } else if tableView == tableview2 {
        cell.textLabel?.text = "Test Table 2 "
        cell.detailTextLabel?.text = "1:30 PM - 2:30 PM"
        cell.selectionStyle = .None

    }

    return cell

}


//**Center collection cells in the middle**

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    let sideInset = (collectionView.frame.size.width - 650) / 2
    return UIEdgeInsets(top: 0, left: sideInset, bottom: 0, right: sideInset)
}

}

//Card Scrolling datasource 

extension SAHomeViewController: UICollectionViewDataSource {

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

    //Number of cards on home screen
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 2
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cardcell", forIndexPath: indexPath)

    // Configure collection view cell

    return cell
}

为了更清楚,这是我的项目编辑器。

enter image description here

最佳答案

您需要在这两个函数中提供默认返回值。因为编译器会检查是否应该返回函数所需的 Int 值,并且在这些函数中,如果任何条件不匹配,它将不会返回任何内容。

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    if tableView == tableView1 {            
        return 2;            
    }
    else if tableView == tableview2
    {            
        return 3;
    }
    return 0; // here
}  

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    if tableView == tableView1 {
        return 2;            
    } else if tableView == tableview2 {            
        return 1;
    }
    return 0; // here
} 

关于ios - TableView 不返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38617712/

相关文章:

ios - 更改 iOS 滑动操作的 bool 值

xcode - 自定义编程 UITableViewCell 的自动布局在滚动时失败

ios - 从特定时间开始嵌入 youtube 视频

iOS 8 今日扩展 : Is it possible to add editable text field?

ios - 停止动态表重新加载 IndexPath - Swift

ios - UITableViewCell 被重用并在 UIView 中显示错误的绘图

ios - UITableViewCell 在使用 xcode6 beta6 界面生成器的 iOS7 和 iOS8 模拟器中表现不同

ios - Azure 通知中心似乎不适用于 ios 10 和 swift 3

cocoa-touch - UILabel TextAlignement 垂直顶部

ios - 让图像 if nil 在 swift 3 中消失(或缩小,使其看起来消失)