结合android ,讲述iOS UITableView和UITableViewCell的用法
2016-08-27 11:27
330 查看
最近刚开始学习 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) // 注册cell到tabview
myFriend.register(nib, forCellReuseIdentifier: "friendliest")
// 注册cell到tabview
let cell:FriendTableViewCell = tableView.dequeueReusableCell(withIdentifier: "friendlist") as! FriendTableViewCell //
实例化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), for: UIControlEvents.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,通过闭包的内容捕获,则可以把索引传给你自己写的方法中!
(不感兴趣的可以跳过这一段内容,直接阅读下一段)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) // 注册cell到tabview
myFriend.register(nib, forCellReuseIdentifier: "friendliest")
// 注册cell到tabview
let cell:FriendTableViewCell = tableView.dequeueReusableCell(withIdentifier: "friendlist") as! FriendTableViewCell //
实例化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), for: UIControlEvents.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,通过闭包的内容捕获,则可以把索引传给你自己写的方法中!
相关文章推荐
- iOS开发14:UITableView与UITableViewCell
- IOS开发中UITableView和UITableViewCell的几种样式
- IOS开发中UITableView和UITableViewCell的几种样式
- iOS开发中UITableView和UITableViewCell的几种样式
- iOS UITableView 用法一:初探
- UITableView和UITableViewCell的一些简单用法
- iOS之UITableView的使用——自定义UITableViewCell
- IOS第七天(1:UiTableView 的基本用法)
- IOS开发中UITableView和UITableViewCell的几种样式
- Swift中UITableView的用法(自定义UITableView和UITableViewCell)
- iOS开发:UITableView与UITableViewCell
- iOS UITableView (类似Android的ListView)(一)
- IOS--UITableView,UITableViewCell介绍
- iOS开发中UITableView和UItableViewCell的几种形式
- iOS tableView 控件用法 [0] UITableView的基本用法
- 给iOS开发新手送点福利,简述UITableView的属性和用法
- iOS开发13:UITableView与UITableViewCell
- ios键盘弹出挡住带自定义UITableViewCell的UITableView的解决方法
- Android之PinnedHeaderExpandableListView- 仿ios的UITableView的header置顶效果
- 【IOS 开发学习总结-OC-51】★★★ios开发之UI控件——UITableView 与UITableViewCell