惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
T
Tenable Blog
TaoSecurity Blog
TaoSecurity Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
AI
AI
P
Proofpoint News Feed
A
About on SuperTechFans
P
Privacy International News Feed
月光博客
月光博客
雷峰网
雷峰网
S
Secure Thoughts
博客园 - 叶小钗
博客园 - 聂微东
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Project Zero
Project Zero
The Cloudflare Blog
SecWiki News
SecWiki News
The Hacker News
The Hacker News
V
Vulnerabilities – Threatpost
罗磊的独立博客
A
Arctic Wolf
阮一峰的网络日志
阮一峰的网络日志
Know Your Adversary
Know Your Adversary
酷 壳 – CoolShell
酷 壳 – CoolShell
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Troy Hunt's Blog
The Last Watchdog
The Last Watchdog
Schneier on Security
Schneier on Security
小众软件
小众软件
有赞技术团队
有赞技术团队
博客园 - 司徒正美
T
Tailwind CSS Blog
量子位
C
Cybersecurity and Infrastructure Security Agency CISA
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Security @ Cisco Blogs
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
L
Lohrmann on Cybersecurity

博客园 - Bruce Lee

Android的界面设计规范-7 Android的界面设计规范-6 Android的界面设计规范-5 Android的界面设计规范-4 Android的界面设计规范-3 Android的界面设计规范-2 Android的界面设计规范-1 Monotouch 移动位于键盘下的内容,自动滚动被键盘遮住的内容 RESTful Web Services,客户端Silverlight提交POST数据报错 Monotouch Copy item from album(从相册拷贝文件出来) Monotouch Save Image To Application(保存相册图片到你的应用程序) Monotouch中使用UINavigationController Monotouch 项目选择Storyborads还是XIBs Montouch 定义一个定制的 View Control类 Monotouch 视图管理周期 Montouch多视图的创建与销毁 Monotouch 常用例子代码 Monotouch在IPAD与IPhone使用UIImagePickerController的图片选择不同的代码 Montouch 增加图片到模拟器(Simulator)的相册中
Monotouch Table View 里面动态增加Table View Cell Switch 触发Switch事件 程序崩溃
Bruce Lee · 2012-09-25 · via 博客园 - Bruce Lee

原始代码

[Export("tableView:cellForRowAtIndexPath:")]
public UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
    var cell=new UITableViewCell();
    cell=tableView.DequeueReusableCell("MyCell");
    if (cell != null)
    {
        UISwitch myUISwitch = new UISwitch ();
        myUISwitch.ValueChanged += delegate {
            string aa = myUISwitch.On.ToString();
        };
        cell.Add (myUISwitch);       
    }
    return cell;
}

可以正常出来Table View Cell,Cell里面也有动态增加进来Swith,但一触发事件,就崩溃。

不动态增加Switch,界面上拖Switch到Cell,也可以正常出来Switch,然后通过

UISwitch mySwitch = (UISwitch)cell.ViewWithTag();也可以获取到Switch,但一触发事件也是崩溃。

原因:

当你GetCell方法返回后,单元格实例不再具有任何引用。是由于被GC收集了。
然而,您的UITableViewCell的本地部分仍然存在(被自身引用的),所以在界面上看起来没问题 - 但是当您使用事件处理程序,它会尝试返回到托管代码...但Switch实例已经不存在了(它会崩溃)。

有几种方法来处理。一种方法是保持每个单元格创建的引用,例如:保存单元格到静态列表<UITableViewCell>。 GC将无法收集他们,这样的事件处理程序稍后将可以找到对象。

说白了就是用静态变量保存住被GC回收的内容。

解决的代码:

[Export("tableView:cellForRowAtIndexPath:")]
public UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
    var cell=new UITableViewCell();
    cell=tableView.DequeueReusableCell("MyCell");
    if (cell != null)
    {
        UISwitch myUISwitch = new UISwitch ();
        UISwitch myUISwitch2 = (UISwitch)cell.ViewWithTag (1000);

        myUISwitch.ValueChanged += delegate {
            string aa = myUISwitch.On.ToString();
        };
        myUISwitch2.ValueChanged += delegate {
            string dd = myUISwitch2.On.ToString();
        };
        cell.AccessoryView = myUISwitch;
        cell.AccessoryView = myUISwitch2;
        cells.Add (cell);       
    }
    return cell;
}

作者:Bruce Lee
出处:http://www.cnblogs.com/BruceLee521
本博原创文章版权归博客园和本人共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出作者名称和原文连接,否则保留追究法律责任的权利。