第三章 点击单元格

本项目是《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