【问题标题】:How to change section location of a UITableViewCell on tap如何在点击时更改 UITableViewCell 的部分位置
【发布时间】:2015-09-16 14:01:23
【问题描述】:

每当点击单元格上的按钮时,我都会尝试将 UITableViewCell 添加到另一个 UITableView 部分。但是,我对如何在单元格已加载到表格视图后更改单元格的部分位置的过程感到非常困惑。目前我有两个部分,并在第一部分中添加了 5 个自定义 UITableViewCells。

关于如何将单元格移动到点击的第二部分的任何想法?

这是我的视图控制器类中的单元格和部分方法:

var tableData = ["One","Two","Three","Four","Five"]

// Content within each cell and reusablity on scroll
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var tableCell : Task =  tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! Task
        tableCell.selectionStyle = .None
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None

    var titleString = "Section \(indexPath.section) Row \(indexPath.row)"
        tableCell.title.text = titleString
    println(indexPath.row)

    return tableCell
}

// Number of sections in table
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}

// Section titles
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
        return "First Section"
    } else {
        return "Second Section"
    }
}

// Number of rows in each section
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return tableData.count
    } else if section == 1 {
        return 0
    } else {
        return 0
    }
}

【问题讨论】:

    标签: ios iphone swift uitableview cocoa-touch


    【解决方案1】:

    您需要为第一部分和第二部分提供单独的数据源。当点击按钮时,修改数据源并使用moveRowAtIndexPath(indexPath: NSIndexPath, toIndexPath newIndexPath: NSIndexPath) UITableView 方法将单元格移动到新部分。

    例如:

    var firstDataSource = ["One","Two","Three","Four","Five"]
    var secondDataSource = [ ]
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return section == 0 ? firstDataSource.count : secondDataSource.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
        cell.textLabel?.text = indexPath.section == 0 ? firstDataSource[indexPath.row] : secondDataSource[indexPath.row]
    
        return cell
    }
    
    // For example, changing section of cell when click on it.
    // In your case, similar code should be in the button's tap event handler
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        if indexPath.section == 0
        {
            let data = firstDataSource[indexPath.row]
    
            tableView.beginUpdates()
            secondDataSource.append(data)
            firstDataSource.removeAtIndex(indexPath.row)
    
            let newIndexPath = NSIndexPath(forRow: find(secondDataSource, data)!, inSection: 1)
    
            tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath)
            tableView.endUpdates()
        }
    }
    

    【讨论】:

    • 哇,太完美了,正是我想要的。如果你不介意我问,你在哪里学习开发 iOS 应用程序,你做了多久了?我仍然是一个初学者,但我很好奇其他人为精通这一点所采取的过程。如果可以的话请告诉我,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多