【问题标题】:custom cell to always be on top of tableView in segmented controller view自定义单元格始终位于分段控制器视图中的 tableView 顶部
【发布时间】:2016-02-17 01:32:54
【问题描述】:

我有一个分段控制器,显示 3 个不同的 tableView,案例 2 创建了一个自定义单元格。我已经让自定义单元格显示在案例 2 中,但新单元格出现在 tableView 的底部。

如何让自定义单元格始终出现在 tableView 的顶部?

@available(iOS 2.0, *)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let myCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)

    switch(mySegmentedControl.selectedSegmentIndex)

    {

    case 0:
        myCell.textLabel!.text = globalList[indexPath.row]
        break

    case 1:
        myCell.textLabel!.text = friendsList[indexPath.row]
        break


    case 2:

    // *** I want to add a Cell here with a "Add Item" IBAction ---------------------

        if(indexPath.row == meList.count) {

            myCell.textLabel?.text = "+ Add Item"
            // add new row here
            /* OR add new button here as cell's subview  */
            // set frame of your button
            // set target and selector method of button
            // [cell addSubView: addButton]

        }
        else {

            myCell.textLabel?.text = meList[indexPath.row]
        }   

    default:
        break

    }

    return myCell

}

【问题讨论】:

    标签: swift tableview uisegmentedcontrol custom-cell


    【解决方案1】:

    我认为您需要做的就是检查indexPath.row == 0 并将按钮添加到该单元格。否则,将单元格配置为正常,但 indexPath 偏移我的。

    if(indexPath.row == 0) {
        myCell.textLabel?.text = "+ Add Item"
        // add new row here
        /* OR add new button here as cell's subview  */
        // set frame of your button
        // set target and selector method of button
        // [cell addSubView: addButton]
    
    } else {
        myCell.textLabel?.text = meList[indexPath.row - 1]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多