• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何在点击时更改 UITableViewCell 的部分位置

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:56:05 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

这是我的 View Controller 类中的单元格和部分方法:

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
    }
}



Best Answer-推荐答案


您需要为第一部分和第二部分提供单独的数据源。点击按钮时,修改数据源并将单元格移动到新的部分,使用 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 - 如何在点击时更改 UITableViewCell 的部分位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32610802/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap