第三章 点击单元格

本文介绍如何在Swift中实现当点击UITableView的Cell时显示UIAlertController,并展示具体实现代码。文章讨论了如何通过addAction方法添加不同的操作选项,如取消、拨打电话提示和标记已访问。同时提到了单元格复用导致的问题及其解决方案。

本项目是《beginning iOS8 programming with swift》中的项目学习笔记==》全部笔记目录

------------------------------------------------------------------------------------------------------------------

实现点击cell弹出alertController

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let alert = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .ActionSheet)
    
    // 取消项
    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
   
    // 打电话项
    let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style: .Default) { (action) -> Void in
        let alertMessage = UIAlertController(title: "Service unavailable", message: "Sorry, the call feature is unavailable yet.", preferredStyle: .Alert)
       
        let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
        alertMessage.addAction(okAction)
        self.presentViewController(alertMessage, animated: true, completion: nil)
    }
   
    // 是否来过
    let isVisitedAction = UIAlertAction(title: "I've been here", style: .Default) { (action) -> Void in
        let cell = tableView.cellForRowAtIndexPath(indexPath)
        cell?.accessoryType = .Checkmark
    }
   
   
    alert.addAction(cancelAction)
    alert.addAction(callAction)
    alert.addAction(isVisitedAction)
   
    self.presentViewController(alert, animated: true, completion: nil)
}

效果图:

 

问题及提高:

1. 现在check了一个cell后,滚动TableView,由于单元格复用,Check会乱掉。提示:使用一个[Bool]数组成员变量记录是否需要check,可以解决。

2. 使用心形图标替换checkmark。 提示:iconImageView.hidden = false 

转载于:https://www.cnblogs.com/tangzhengyue/p/4303495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值