ios - 在单个 View 中控制多个 TableView

标签 ios swift uitableview

我有一个嵌套在 Collection View 中的表格 View ,我返回了 3 个(将来可能更多) Collection View 单元格,我想知道是否可以在每个集合单元格中呈现不同的内容?我附上了一些屏幕截图,以更好地理解我在做什么。谢谢。

enter image description here

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 1
}


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

    // Configure the cell...

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

最佳答案

是的,你可以。您需要为您拥有的每个 tableView 设置一个属性,并在 delegate 方法中比较它,如下所示

class Some: UIViewController {
    var firstTableView: UITableView
    var secondTableView: UITableView

    override func viewDidLoad() {
        firstTableView = YOUR_FIRST
        secondTableView = YOUR_Second
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        if tableView == firstTableView {
            return 2;
        }
        else if tableView == secondTableView {
            return 1;
        }
        return 3
    }

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


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
        if tableView == firstTableView {
            cell = tableView.dequeueReusableCellWithIdentifier("cellOfFirstTableView", forIndexPath: indexPath) as! UITableViewCell
        }
        else if tableView == secondTableView {
            cell = tableView.dequeueReusableCellWithIdentifier("cellOfSecondTableView", forIndexPath: indexPath) as! UITableViewCell
        }
        // Configure the cell...
        if tableView == firstTableView {
            cell.textLabel?.text = "Homeroom"
            cell.detailTextLabel?.text = "8:15 AM - 9:00 AM"
            cell.selectionStyle = .None
        }
        else if tableView == secondTableView {
            cell.textLabel?.text = "Homeroom"
            cell.detailTextLabel?.text = "8:15 AM - 9:00 AM"
            cell.selectionStyle = .None
        }

        return cell
    }
}

关于ios - 在单个 View 中控制多个 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38560097/

相关文章:

ios - 更改带有时间延迟的 UITextField 文本

ios - Firebase 集成到 IOS 项目 - 没有这样的文件或目录

ios - 如何使用 Storyboard在 iOS 中制作水平和垂直滚动的 TableView?

iphone - 添加到收藏夹功能?

iphone - 从 ViewController 设置 UIImageView 图像

ios - SpriteKit : node Y position and touch Y position not consistent

iphone - 声音类中的委托(delegate)如何导致 Controller 类中按钮图像的更改?

通过变量快速访问结构

swift - 在 tableviewcell 上覆盖按钮,里面有 collectionview

swift - 在 Xcode 中使用 Parse 和 Swift 连接 PFUITableView 和详细信息 View