我是一名学生 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
}
为了更清楚,这是我的项目编辑器。
最佳答案
您需要在这两个函数中提供默认返回值。因为编译器会检查是否应该返回函数所需的 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/