结合android ,讲述iOS UITableView和UITableViewCell的用法

     最近刚开始学习 ios ,以前做了一年多的 andorid ,对于 ios 来说上手还是比较快的,因为他们的大体设计思路和实现方式是一样的,个人觉得还是 android 的架构设计更加符合人类的思维方式(哈哈吐槽下苹果的工程师),今天我拿 andorid 的 Listview 对比讲述下 ios 的 UITableView 的具体使用方法。

     (不感兴趣的可以跳过这一段内容,直接阅读下一段)andoird 中实现列表页是使用的Listview这个控件,要想让它显示出内容,就必须使用内容适配器adapter,伪代码:listview.setAdapter(myAdapter);在adapter里面完成每一行数据的绑定和列表项的操作, ios 和 android 的实现思路是差不多的,下面就来讲述 ios 的实现。

      我们用到的 UITableView 就相当于 android 的 Listview, UITableViewCell 就相当于BaseAdapter ,直接上代码,关键代码看注释:

class FriendViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {   // 实现两个协议 

    

    @IBOutlet weak var myFriend: UITableView!

    

    var friends :Array<FriendModel> = [];

    

    override func viewDidLoad() {

        super.viewDidLoad()

        initFriend();

        myFriend.dataSource = self;      // 托管,

        myFriend.delegate = self;       // 托管,

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

    

   

     /*

      UITableViewDelegate协议的方法,设置行高

     */

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{   

        return 100.0;

    }

    

    

     /*

     UITableViewDataSouth协议的方法,设置行数

     */

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

        return friends.count;

    }

   /*

     UITableViewDataSouth协议的方法,实例化cell,并且做赋值操作

     */ 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

       

        let obj :FriendModel = friends[indexPath.row];

        let nib:UINib = UINib(nibName:"FriendTableViewCell", bundle: nil// 注册celltabview

        myFriend.register(nib, forCellReuseIdentifier: "friendliest")    // 注册celltabview

        let cell:FriendTableViewCell = tableView.dequeueReusableCell(withIdentifier: "friendlist"asFriendTableViewCell // 实例化cell

        cell.headView.image = UIImage(named: obj.img);

        cell.name.text = obj.name;

        cell.content.text = obj.content;

        cell.time.text = obj.time;

        cell.deletebtn.tag = indexPath.row//给按钮设置tag,        

        cell.deletebtn.addTarget(self, action: #selector(FriendViewController.delectAction), forUIControlEvents.touchUpInside);//设置点击

        return cell;

    }

    

    /*

     删除按钮的点击事件

     */

  

    func delectAction(btn : UIButton){

        

        

        let alertController = UIAlertController(title: "提示", message:    "确认删除此项目嘛?\(btn.tag)", preferredStyle:  //设置弹窗 UIAlertControllerStyle.alert); 

        alertController.addAction(UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel,handler: nil));          

        

        alertController.addAction(UIAlertAction(title: "确定", style:  UIAlertActionStyle.destructive) {               //弹窗确定事件

            

            (action:UIAlertAction) -> Void in                            //闭包,实现具体操作,移除相应的数据并刷新列表

            self.friends.remove(at: btn.tag);

            self.myFriend.reloadData();

            

            } 

         );     

        self.present(alertController, animated: true, completion: nil);     //弹出提示窗口

    }

    

    /*

     table项的点击事件

    */

    

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

        let vc  = ChatViewController();

        self.present(vc, animated: true, completion: nil);

    }


    

    

    func initFriend(){

        

        let friend1 = FriendModel(time: "2016-07-05",img: "head1",name: "小黑",content: "山东青岛");

        let friend2 = FriendModel(time: "2016-07-27",img: "head2",name: "小明",content: "山东东营");

        let friend3 = FriendModel(time: "2016-07-27",img: "head",name: "H.HELO",content: "河北廊坊");

        let friend4 = FriendModel(time: "2016-04-27",img: "head4",name: "Tommor",content: "美国阿拉斯加");

        

         friends.append(friend1);

         friends.append(friend2);

         friends.append(friend3);

         friends.append(friend4);

    

    }

    



    以上代码就完成了 ios 列表页面


    注意事项 :点击列表做操作想把indexPath(点击位置索引)传给自定义的方法,在cell赋值时候可以通过给button 设置tag的值,它是整形的,然后再点击的时候取出相应button的tag,通过闭包的内容捕获,则可以把索引传给你自己写的方法中!


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值