显示更多在uitableview中显示

时间:2015-12-14 12:21:51

标签: ios swift uitableview

默认情况下,每个部分应显示最多三个单元格。如果任何单元格包含三个以上的单元格,则应显示“显示更多”选项。如果显示更多单元格,我想显示该特定部分中的其余单元格。我花了两天时间,没有任何成功。桌子顶部的部分。取决于所选的段,tableview加载单元格。每个部分的每个部分的代码都有所不同,因此函数cellForRowAtIndexPath:变得非常大。我大致添加了代码。这段代码就是我试过的。

if segmentName == .Feature || segmentName == .Services
    {
        if indexPath.section == 0
        {
            if boolShowFullFeature[indexPath.section] == false
            {
                if indexPath.row == showCells
                {
                    return createShowMoreCell(indexPath.section)
                }
                else
                {
                    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SearchListViewCell
                    var firstTitle = "", secondTitle = "", thirdTitle = "", recomendValue = "", starValue = ""

                    (firstTitle, secondTitle, thirdTitle, recomendValue, starValue) = model.foodValue[indexPath.row]
                    cell.configureCell(firstTitle, secondTitle: secondTitle, thirdTitle: thirdTitle, recomendValue: recomendValue, starValue: starValue)
                    return cell

1 个答案:

答案 0 :(得分:0)

实际上我以前做过这个。这是示例代码:

var objects = [["1","2","3"],["q","w","e","r","t","y"],["z","x","c","v","b","n","m"]]
var sec = ["sec a","sec b","sec c"]
var showallSec = 0

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return sec.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let items = objects[section].count

    if items > 3{
        if section == showallSec-1{
            return items
        }
        return 4
        }
    return items
}

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

    let object = objects[indexPath.section][indexPath.row]
    cell.textLabel!.text = object
    if(indexPath.section != showallSec-1){
    if(indexPath.row == 3){
        cell.textLabel!.text = "show more"
        }}
    return cell
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sec[section]
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if(indexPath.row == 3){
        showallSec = indexPath.section + 1  
        tableView.reloadData()
    }
}