UITableView
就是表格,但是只有行没有列
创建格式有Plain和Group
而实现函数则有两个代理
delegate, 处理tableview中的逻辑关系
1.创建
设置全局变量
var _MyTableView : UITableView? //全局的Tableview var leftBtn:UIButton? //左边按钮,用于删除等操作 var rightButtonItem:UIBarButtonItem? //右边按钮,用于添加 var items = ["武汉","上海","北京","深圳","广州","重庆","香港","台海","天津"]创建TableView
/*重用机制 当表行滑出屏幕时讲其UITableViewCell放入到重用队列中 当表行重新划入屏幕时,则将其UITableViewCell从重用队列中取出来 由此减少每次对生成和施放内存的操作 */ func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cellid = "my cellid" //tableview行的标志 var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellid,forIndexPath: indexPath)//获取行标志 if (cell == nil){ cell = UITableViewCell(style:.Default,reuseIdentifier: cellid) //空的话则重新创建一个,cellid表示新创建的UITableViewCell } cell!.textLabel?.text = self.items[indexPath.row] cell!.imageView?.image = UIImage(named: "green.png") return cell! //返回 }重写初始化函数
//父类的初始化函数 override func viewDidLoad() { super.viewDidLoad() let Rc:CGRect = self.view.bounds _MyTableView = UITableView(frame: Rc,style:UITableViewStyle.Plain) _MyTableView!.delegate = self _MyTableView!.dataSource = self //setupLeftBarButtonItem() setupRightBarButtonItem() self.view.addSubview(_MyTableView!) }
设置表格中的数量
//返回表中的数量 func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int{ return items.count }
//设置点击事件 func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath){ //let alert: UIAlertController = UIAlertController(title: "提示",message: "你选择的是\(self.dataArr[indexPath.row])",preferredStyle: UIAlertControllerStyle.Alert) //let okAction = UIAlertAction(title: "好的",style: UIAlertActionStyle.Default,handler: nil) // alert.addAction(okAction) // self.presentViewController(alert,animated: true,completion: nil) }
添加右边按钮和左边按钮以及其事件
//加左边按钮 func setupLeftBarButtonItem() { leftBtn = UIButton(type: UIButtonType.Custom ) self.leftBtn!.frame = CGRectMake(0,50,40) self.leftBtn?.setTitleColor(UIColor.redColor(),forState: UIControlState.Normal) self.leftBtn?.setTitle("Edit",forState: UIControlState.Normal) self.leftBtn!.tag = 100 self.leftBtn?.addTarget(self,action: "leftBarButtonItemClicked",forControlEvents: UIControlEvents.TouchUpInside) let barButtonItem = UIBarButtonItem(customView: self.leftBtn!) self.navigationItem.leftBarButtonItem = barButtonItem } //左边按钮事件 func leftBarButtonItemClicked() { print("leftBarButton") if (self.leftBtn!.tag == 100) { self._MyTableView?.setEditing(true,animated: true) self.leftBtn!.tag = 200 self.leftBtn?.setTitle("Done",forState: UIControlState.Normal) //将增加按钮设置不能用 self.rightButtonItem!.enabled=false } else { //恢复增加按钮 self.rightButtonItem!.enabled=true self._MyTableView?.setEditing(false,animated: true) self.leftBtn!.tag = 100 self.leftBtn?.setTitle("Edit",forState: UIControlState.Normal) } } //加右边按钮 func setupRightBarButtonItem() { self.rightButtonItem = UIBarButtonItem(title: "Add",style: UIBarButtonItemStyle.Plain,target: self,action: "rightBarButtonItemClicked") self.navigationItem.rightBarButtonItem = self.rightButtonItem } //增加事件 func rightBarButtonItemClicked() { print("rightBarButton") let row = self.items.count let indexPath = NSIndexPath(forRow:row,inSection:0) //self.items.append("杭州") self._MyTableView?.insertRowsAtIndexPaths([indexPath],withRowAnimation: UITableViewRowAnimation.Left) }
设置分组的TableView
初始化时将风格设置为Group,并且在设置cell名称是应该使用多个数组
<pre name="code" class="plain"><p class="p1"><span class="s1">var</span><span class="s2"> items = [[</span><span class="s3">"</span><span class="s4">武汉</span><span class="s3">"</span><span class="s2">,</span><span class="s3">"</span><span class="s4">上海</span><span class="s3">"</span><span class="s2">,</span><span class="s3">"</span><span class="s4">北京</span><span class="s3">"</span><span class="s2">],[</span><span class="s3">"</span><span class="s4">深圳</span><span class="s3">"</span><span class="s2">,</span><span class="s3">"</span><span class="s4">广州</span><span class="s3">"</span><span class="s2">,</span><span class="s3">"</span><span class="s4">重庆</span><span class="s3">"</span><span class="s2">],[</span><span class="s3">"</span><span class="s4">香港</span><span class="s3">"</span><span class="s2">,</span><span class="s3">"</span><span class="s4">台海</span><span class="s3">"</span><span class="s2">,</span><span class="s3">"</span><span class="s4">天津</span><span class="s3">"</span><span class="s2">]]</span></p>
self.tableView=UITableView(frame:self.view.frame,style:UITableViewStyle.Grouped)
//总行数 func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int{ return self.items[section].count - 1 } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return items.count }
func tableView(tableView: UITableView,titleForHeaderInSection section: Int) -> String?{ return items[section][0] }