【问题标题】:Set added row as first indexPath row将添加的行设置为第一个 indexPath 行
【发布时间】:2016-02-17 10:10:21
【问题描述】:

我有 3 个分段控制器视图,其中一个我在表格视图中添加了一个额外的自定义单元格。我的问题是如何在其中一种情况下设置自定义单元格,以便在视图加载时始终设置在 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

}

【问题讨论】:

    标签: ios xcode swift tableview uisegmentedcontrol


    【解决方案1】:

    首先,在tableView(_:numberOfRowsInSection:) 中,您需要+ 1 您返回的数字,以便多出一行。 (仅当 mySegmentedControl.selectedSegmentIndex == 2 时才这样做。)

    然后在tableView(_:cellForRowAtIndexPath:),如果mySegmentedControl.selectedSegmentIndex == 2indexPath.row == 0(第一个单元格),配置并返回您的“添加项目”单元格。

    这一额外行的插入将使您的所有索引偏移 1。因此在else 语句中,您需要myCell.textLabel?.text = meList[indexPath.row - 1]。这样,例如,当indexPath.row == 1 时,您将返回数组的第一个元素(第 0 个索引)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 2018-10-04
      • 2011-10-09
      • 2016-09-18
      • 1970-01-01
      • 2020-09-13
      • 2014-08-31
      相关资源
      最近更新 更多