Mic*_*ich 67 iphone cocoa-touch objective-c
有没有办法UITableView从一个内部访问拥有UITableViewCell?
jbr*_*nan 117
weak在单元格中存储对tableView 的引用,您将在-tableView:cellForRowAtIndexPath:表格的dataSource中设置该引用.
这比依赖self.superview始终完全是tableView更脆弱更好.谁知道Apple UITableView将来如何重新组织视图层次结构.
DTs*_*DTs 18
这是一种更好的方法,它不依赖于任何特定的UITableView层次结构.它可以与任何未来的iOS版本一起使用,前提是UITableView它不会完全改变类名.这不仅极不可能,但如果真的发生了,你无论如何都要修饰你的代码.
只需导入下面的类别并获取您的参考 [myCell parentTableView]
@implementation UIView (FindUITableView)
-(UITableView *) parentTableView {
// iterate up the view hierarchy to find the table containing this cell/view
UIView *aView = self.superview;
while(aView != nil) {
if([aView isKindOfClass:[UITableView class]]) {
return (UITableView *)aView;
}
aView = aView.superview;
}
return nil; // this view is not within a tableView
}
@end
Run Code Online (Sandbox Code Playgroud)
// To use it, just import the category and invoke it like so:
UITableView *myTable = [myTableCell parentTableView];
// It can also be used from any subview within a cell, from example
// if you have a UILabel within your cell, you can also do:
UITableView *myTable = [myCellLabel parentTableView];
// NOTE:
// If you invoke this on a cell that is not part of a UITableView yet
// (i.e., on a cell that you just created with [[MyCell alloc] init]),
// then you will obviously get nil in return. You need to invoke this on cells/subviews
// that are already part of a UITableView.
Run Code Online (Sandbox Code Playgroud)
更新
评论中有一些讨论是否保持弱引用是一种更好的方法.这取决于你的情况.遍历视图层次结构时会有一些小的运行时损失,因为您正在循环直到识别出目标UIView.你的观点有多深?另一方面,保持对每个单元格的引用具有最小的内存惩罚(弱引用毕竟是一个指针),并且通常在不需要它们的地方添加对象关系被认为是一个糟糕的OO设计实践,原因很多,并且应该应避免(见下面评论中的详细信息).
更重要的是,将表引用保留在单元格内会增加代码复杂性并导致错误,因为UITableViewCells它们是可重用的.UIKit不包括cell.parentTable财产并非巧合.如果您定义自己的代码,则必须添加代码来管理它,如果您没有有效地执行此操作,则可能会引入内存泄漏(即,单元格在其表的生命周期内存活).
因为通常当用户与单元格交互时(单个单元格执行),而不是在布置表格时[tableView:cellForRowAtIndexPath:](对所有可见单元格执行),您将使用上面的类别,运行时成本应该是无关紧要的.
Cul*_*tes 13
Xcode 7 beta,Swift 2.0
这对我来说很好,在我看来它与层次结构或其他什么都没有关系.到目前为止,我对这种方法没有任何麻烦.我已经将它用于许多异步回调(例如,当API请求完成时).
TableViewCell类
class ItemCell: UITableViewCell {
var updateCallback : ((updateList: Bool)-> Void)? //add this extra var
@IBAction func btnDelete_Click(sender: AnyObject) {
let localStorage = LocalStorage()
if let description = lblItemDescription.text
{
//I delete it here, but could be done at other class as well.
localStorage.DeleteItem(description)
}
updateCallback?(updateList : true)
}
}
Run Code Online (Sandbox Code Playgroud)
在实现DataSource和Delegate的表视图类中
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: ItemCell = self.ItemTableView.dequeueReusableCellWithIdentifier("ItemCell") as! ItemCell!
cell.updateCallback = UpdateCallback //add this extra line
cell.lblItemDescription?.text = self.SomeList[indexPath.row].Description
return cell
}
func UpdateCallback(updateTable : Bool) //add this extra method
{
licensePlatesList = localStorage.LoadNotificationPlates()
LicenseTableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
当然你可以在其中放置任何变量updateCallback并相应地改变它的功能tableView.
有人可能想告诉我它是否可以保存使用,只是为了确定.
在构造表视图单元格时,必须将引用添加回UITableView.
但是,几乎可以肯定你真正想要的是对你的UITableViewController的引用......它需要相同的东西,在你构建单元格时将它设置为单元格的委托并将其交给表格视图.
如果要进行连接操作,另一种方法是在IB中构建单元格,将表视图控制器作为文件所有者 - 然后将单元格中的按钮连接到表视图控制器中的操作.使用loadNibNamed加载单元格xib时,将视图控制器作为所有者传入,按钮操作将连接回表视图控制器.
| 归档时间: |
|
| 查看次数: |
48401 次 |
| 最近记录: |