在同一个uitableview中显示两个不同的自定义单元格 - swift firebase [英] Show two different custom cells in same uitableview - swift firebase

查看:123
本文介绍了在同一个uitableview中显示两个不同的自定义单元格 - swift firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



到目前为止我管理的是接收更新到更新单元格,称为 cell 。我只是不知道如何获得numberOfRowsInSection返回两个值,所以我的单元格将显示。



让我通过我的代码解释:
$ b

 重写func numberOfSectionsInTableView(tableView:UITableView) - > Int {
return 1
}

覆盖func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int {
return updates.count
return updatesTask.count //我不能这样做 - 我能做些什么呢?
}



覆盖func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {
let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier(Cell,forIndexPath:indexPath)as! updateTableViewCell
let cellTask​​:tasksTableViewCell = tableView.dequeueReusableCellWithIdentifier(TaskCell,forIndexPath:indexPath)as! tasksTableViewCell


let update = updates [indexPath.row]
let updateTask = updatesTask [indexPath.row]


//示例这两个不同的单元格需要不同的firebase数据
cell.nameLabel.text = update.addedByUser
cellTask​​.nameLabel.text = updateTask.addedByUser

正如你可能看到的那样, let updateTask 试图获得一个 indexPath .row 但这是不可能的,因为我不能有两个返回值在 numberOfRowsInSection ,这是一个问题,因为这个数字是指数据存储在我的firebase数据库的地方..我怎样才能修改这个工作?



希望你们明白我要去哪里,否则让我知道,我会尽力解释: - )

解决方案

如果你想把他们放在两个教派离子。



这是如果你想全部在一个部分的解决方案。



首先,在 numberOfRowsInSection 方法你需要返回这两个数组的总数,像这样: return(updates.count + updatesTask.count)

然后你需要像这样配置 cellForRowAtIndexPath 方法:

 覆盖func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {

if indexPath.row< updates.count {
//更新
let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier(Cell,forIndexPath:indexPath)as! updateTableViewCell
let update = updates [indexPath.row]
cell.nameLabel.text = update.addedByUser
return cell
} else {
// UpdatesTask
让cellTask​​:tasksTableViewCell = tableView.dequeueReusableCellWithIdentifier(TaskCell,forIndexPath:indexPath)as! tasksTableViewCell
let updateTask = updatesTask [indexPath.row-updates.count]
cellTask​​.nameLabel.text = updateTask.addedByUser
return cellTask​​
}

}

这将显示所有cellTask​​s之后的所有单元。





如果更新数组和 updatesTask 数组具有相等您可以使用这个:

 重写func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {

if indexPath.row%2 == 0 {
//更新
let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier(Cell,forIndexPath:indexPath)as! updateTableViewCell
let update = updates [indexPath.row / 2]
cell.nameLabel.text = update.addedByUser
return cell
} else {
// UpdatesTask
let cellTask​​:tasksTableViewCell = tableView.dequeueReusableCellWithIdentifier(TaskCell,forIndexPath:indexPath)as! tasksTableViewCell
let updateTask = updatesTask [indexPath.row / 2]
cellTask​​.nameLabel.text = updateTask.addedByUser
return cellTask​​
}

}


I am currently having a problem with displaying two different types of custom cells on the same uitableview.

What I have managed so far, is receiving the "updates" to the update cell, known as cell. I just cannot figure out how to also get numberOfRowsInSection to return two values, so both of my cells will show.

Let me explain through my code:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return updates.count
        return updatesTask.count // I CANNOT DO THIS - what can I do instead?
    }



    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell
        let cellTask:tasksTableViewCell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! tasksTableViewCell


        let update = updates[indexPath.row]
        let updateTask = updatesTask[indexPath.row]


        // Example of the two different cells that need different data from firebase
        cell.nameLabel.text = update.addedByUser
        cellTask.nameLabel.text = updateTask.addedByUser

As you can probably see, the let updateTask is trying to get an indexPath.row but that is not possible, since I cannot have two return values in the numberOfRowsInSection, which is a problem because that number is referring to the place where the data is stored in my firebase database.. How can I modify this to make it work?

Hope you guys understand where I am going with this, otherwise let me know and I will try to explain better :-)

解决方案

@Callam's answer is great if you want to put them in two sections.

This is the solution if you want all to be in one section.

First, in numberOfRowsInSection method you need to return the sum of those two array counts like this: return (updates.count + updatesTask.count)

Then you need to configure cellForRowAtIndexPath method like this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row < updates.count{
        // Updates
        let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell
        let update = updates[indexPath.row]
        cell.nameLabel.text = update.addedByUser
        return cell
    } else {
        // UpdatesTask
        let cellTask:tasksTableViewCell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! tasksTableViewCell
        let updateTask = updatesTask[indexPath.row-updates.count]
        cellTask.nameLabel.text = updateTask.addedByUser
        return cellTask
    }

}

This will display all cells followed by all cellTasks.


If updates array and updatesTask array have equal number of items and you want to display them one by one you can use this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row % 2 == 0 {
        // Updates
        let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell
        let update = updates[indexPath.row/2]
        cell.nameLabel.text = update.addedByUser
        return cell
    } else {
        // UpdatesTask
        let cellTask:tasksTableViewCell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! tasksTableViewCell
        let updateTask = updatesTask[indexPath.row/2]
        cellTask.nameLabel.text = updateTask.addedByUser
        return cellTask
    }

}

这篇关于在同一个uitableview中显示两个不同的自定义单元格 - swift firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆