UITableViewCell作为参数不是副本吗? [英] Is a UITableViewCell as parameter not a copy?

查看:240
本文介绍了UITableViewCell作为参数不是副本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许现在还为时过早,但是我有一些我无法理解的代码。

Maybe it's just too early for now but I got a little piece of code I can't follow.

在UITableViewController中是以下

In a UITableViewController is the following

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = printTable.dequeueReusableCellWithIdentifier("printCell", forIndexPath: indexPath) as UITableViewCell
    configureTestCell(cell, atIndexPath: indexPath)

    return cell;
}

configureTestCell函数:

The configureTestCell function:

func configureTestCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath)
{
    let printCell = cell as PrintCell

    if (self.searchActive)
    {
        printCell.nameLabel.text = "Project \(filteredData[indexPath.item])"
    }
    else
    {
        printCell.nameLabel.text = "Project \(printData[indexPath.item])"
    }
}

所以我的问题是,为什么在printCell中所做的更改会影响tableView函数中的单元格对象?

So my problem and question here is, why are the changes made in printCell have an effect on the cell object in the tableView function? Isn't the cell just a copy or am I missing something stupid-easy?

推荐答案

对于对象,我们可以通过引用传递它和价值。
当您编写

For objects we can pass it by reference and by value. When you write

let printCell = cell as PrintCell

通过引用,它意味着printCell将没有新的内存分配,它将指向单元格本身的内存位置。

Its by reference, means printCell will not have new memory allocation, it will point to memory location of cell itself.

现在当您执行printCell操作时,它将反映在您的视图中,因为两个对象都指向相同的内存位置。

Now when you perform operation of printCell it will be reflected in your view, As the both objects point to the same memory location.

如果通过使用copy关键字的值(我不遵循Objective-c,我不知道如何迅速完成操作。)

If you assign object by value using copy keyword (I don't know how to do it with swift as I am following objective-c).

它将具有新的内存位置,并且在您执行时printCell上的任何任务都不会反映在Tableview中。

It will have new memory location and when you perform any task on printCell it will not reflected in Tableview.

这篇关于UITableViewCell作为参数不是副本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆