iOS开发——UI篇Swift篇&玩转UItableView(一)基本使用

UItableView基本使用

  1 class ListViewController: UIViewController , UITableViewDataSource, UITableViewDelegate {

  2     

  3     //定义listTableView 输出口

  4     @IBOutlet var listTableView : UITableView!

  5     

  6     //定义数组

  7     var items:[String] = ["UITableView高级使用",

  8         "自定义UITableViewCell",

  9         "创建分组表",

 10         "UITextView",

 11         "UISegmentedControl",

 12         "UISlider",

 13         "UISwitch和UIStepper",

 14         "UIActivityIndicatorView",

 15         "UIProgressView",

 16         "UIAlertView",

 17         "UIActionSheet",

 18         "UIImageView",

 19         "UIScrollView和UIPageControl",

 20         "UIWebView",

 21         "UIDatePicker",

 22         "UIPickerView",

 23         "UIToolBar 和 UISearchBar",

 24         "UINavigationController",

 25         "UITabBarController",

 26         "读写应用程序数据",

 27         "多点触摸和手势识别",

 28         "多媒体应用",

 29         "动画",

 30         "网络通信",

 31         "检测设备朝向和移动",

 32         "定位处理与地图",

 33         "地址簿、短信、邮件",

 34         "iOS应用本地化"]

 35 

 36     

 37     

 38     

 39     override func viewDidLoad() {

 40         super.viewDidLoad()

 41        

 42         // Do any additional setup after loading the view.

 43         

 44 //        //代码创建TabelView

 45 //        var listTableView : UITableView = UITableView(frame: CGRectMake(0, 0, 300, 600), style: UITableViewStyle.Grouped)

 46         

 47         //指定TabelView的数据源和代理

 48         self.listTableView.dataSource = self

 49         self.listTableView.delegate = self

 50     }

 51 

 52     override func didReceiveMemoryWarning() {

 53         super.didReceiveMemoryWarning()

 54         // Dispose of any resources that can be recreated.

 55     }

 56     

 57 

 58     

 59     //MARK: - UITableViewDelegate

 60     

 61     //tableView数据源:返回几节(组)

 62     func numberOfSectionsInTableView(tableView: UITableView) -> Int

 63     {

 64         return 1

 65     }

 66     

 67     //tableView数据源:返回行数

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

 69     {

 70         return items.count

 71     }

 72     

 73     //tableView 数据源:每一行高度

 74     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat

 75     {

 76         return 50

 77     }

 78     

 79     //tableView数据源:每一行内容

 80     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

 81     {

 82         //Cell标示符,代表一系列

 83         // OC:使用static,  swift:使用let

 84         let cellIdentifier: String = "cellIdentifier"

 85         

 86         //通过cellIdentifier标示符取没有使用的Cell

 87         //有可能不存在,所以使用:optional

 88         var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell

 89         

 90         //如果cell取到是空

 91         if cell == nil { // no value

 92             

 93             //创建新的cell

 94             //cell样式:UITableViewCellStyle.Default

 95             //cell标示符:cellIdentifier

 96             cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)

 97             

 98             //设置字体

 99 //             cell!.textLabel?.font = UIFont.systemFontOfSize(14)

100             //2015年4月10号修改

101             cell!.textLabel?.font = UIFont.systemFontOfSize(14)

102             

103             //设置选中cell样式

104             cell!.selectionStyle = .Gray;

105             

106             //设置cell后面箭头样式

107             cell!.accessoryType = UITableViewCellAccessoryType.DetailButton;

108         }

109         

110         //从数组取对应值给cell赋值

111 //        cell!.textLabel?.text = self.items[indexPath.row]

112         //2015年4月10号修改

113         cell!.textLabel?.text = self.items[indexPath.row]

114         

115         //设置cell图片

116 //        cell!.imageView.image = UIImage(named:"cellImage.png")

117         //2015年4月10号修改

118         cell!.imageView?.image = UIImage(named:"cellImage.png")

119         

120         cell!.detailTextLabel?.text = "详细信息介绍";

121         

122         return cell!;

123     }

124     

125     

126     //tableView代理:点击一行

127     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

128     {

129         //2015年4月10号:将此处所有textLabel?.   修改成textLabel?.

130         

131         //释放选中效果

132         tableView.deselectRowAtIndexPath(indexPath, animated: true)

133         

134         //获得点击的cell

135         let cell:UITableViewCell! = tableView.cellForRowAtIndexPath(indexPath)

136         println(cell!.textLabel?.text!)

137         

138         var viewCtl : UIViewController!

139         

140         //判断cell内容,根据内容不同打开不同界面

141         if cell!.textLabel?.text! == "UITableView高级使用"

142         {

143             var newCtl = UITableViewControllerAF(nibName: "UITableViewControllerAF", bundle: NSBundle.mainBundle())

144             newCtl.titleString = cell.textLabel?.text!

145             viewCtl = newCtl

146         }

147         else if cell.textLabel?.text == "自定义UITableViewCell"

148         {

149             var newCtl = UITableViewControllerCustom(nibName: "UITableViewControllerCustom", bundle: NSBundle.mainBundle())

150             newCtl.titleString = cell.textLabel?.text

151             viewCtl = newCtl

152         }

153         else if cell.textLabel?.text == "创建分组表"

154         {

155             var newCtl = UITableViewControllerGroup(nibName: "UITableViewControllerGroup", bundle: NSBundle.mainBundle())

156             newCtl.titleString = cell.textLabel?.text

157             viewCtl = newCtl

158         }

159         else if cell.textLabel?.text == "UITextView"

160         {

161             var newCtl = UITextViewController(nibName: "UITextViewController", bundle: NSBundle.mainBundle())

162             newCtl.titleString = cell.textLabel?.text

163             viewCtl = newCtl

164         }

165         else if cell.textLabel?.text == "UISegmentedControl"

166         {

167             var newCtl = UISegmentedControlController(nibName: "UISegmentedControlController", bundle: NSBundle.mainBundle())

168             newCtl.titleString = cell.textLabel?.text

169             viewCtl = newCtl

170         }

171         else if cell.textLabel?.text == "UISlider"

172         {

173             var newCtl = UISliderController(nibName: "UISliderController", bundle: NSBundle.mainBundle())

174             newCtl.titleString = cell.textLabel?.text

175             viewCtl = newCtl

176         }

177         else if cell.textLabel?.text == "UISwitch和UIStepper"

178         {

179             var newCtl = UISwitchUIStepperController(nibName: "UISwitchUIStepperController", bundle: NSBundle.mainBundle())

180             newCtl.titleString = cell.textLabel?.text

181             viewCtl = newCtl

182         }

183         else if cell.textLabel?.text == "UIActivityIndicatorView"

184         {

185             var newCtl = UIActivityIndicatorViewController(nibName: "UIActivityIndicatorViewController", bundle: NSBundle.mainBundle())

186             newCtl.titleString = cell.textLabel?.text

187             viewCtl = newCtl

188         }

189         else if cell.textLabel?.text == "UIProgressView"

190         {

191             var newCtl = UIProgressViewController(nibName: "UIProgressViewController", bundle: NSBundle.mainBundle())

192             newCtl.titleString = cell.textLabel?.text

193             viewCtl = newCtl

194         }

195         else if cell.textLabel?.text == "UIImageView"

196         {

197             var newCtl = UIImageViewController(nibName: "UIImageViewController", bundle: NSBundle.mainBundle())

198             newCtl.titleString = cell.textLabel?.text

199             viewCtl = newCtl

200         }

201         else if cell.textLabel?.text == "UIScrollView和UIPageControl"

202         {

203             var newCtl = UIScrollViewUIPageControl(nibName: "UIScrollViewUIPageControl", bundle: NSBundle.mainBundle())

204             newCtl.titleString = cell.textLabel?.text

205             viewCtl = newCtl

206         }

207         else if cell.textLabel?.text == "UIWebView"

208         {

209             var newCtl = UIWebViewController(nibName: "UIWebViewController", bundle: NSBundle.mainBundle())

210             newCtl.titleString = cell.textLabel?.text

211             viewCtl = newCtl

212         }

213         else if cell.textLabel?.text == "UIDatePicker"

214         {

215             var newCtl = UIDatePickerController(nibName: "UIDatePickerController", bundle: NSBundle.mainBundle())

216             newCtl.titleString = cell.textLabel?.text

217             viewCtl = newCtl

218         }

219         else if cell.textLabel?.text == "UIPickerView"

220         {

221             var newCtl = UIPickerViewController(nibName: "UIPickerViewController", bundle: NSBundle.mainBundle())

222             newCtl.titleString = cell.textLabel?.text

223             viewCtl = newCtl

224         }

225         else if cell.textLabel?.text == "UIAlertView"

226         {

227             var newCtl = UIAlertViewController(nibName: "UIAlertViewController", bundle: NSBundle.mainBundle())

228             newCtl.titleString = cell.textLabel?.text

229             viewCtl = newCtl

230         }

231         else if cell.textLabel?.text == "UIActionSheet"

232         {

233             var newCtl = UIActionSheetController(nibName: "UIActionSheetController", bundle: NSBundle.mainBundle())

234             newCtl.titleString = cell.textLabel?.text

235             viewCtl = newCtl

236         }

237         else if cell.textLabel?.text == "UIToolBar 和 UISearchBar"

238         {

239             var newCtl = UIToolBarUISearchBar(nibName: "UIToolBarUISearchBar", bundle: NSBundle.mainBundle())

240             newCtl.titleString = cell.textLabel?.text

241             viewCtl = newCtl

242         }

243         else if cell.textLabel?.text == "UINavigationController"

244         {

245             var newCtl = UINavigationControllerController(nibName: "UINavigationControllerController", bundle: NSBundle.mainBundle())

246             newCtl.titleString = cell.textLabel?.text

247             viewCtl = newCtl

248         }

249         else if cell.textLabel?.text == "UITabBarController"

250         {

251             var newCtl = UITabBarControllerController(nibName: "UITabBarControllerController", bundle: NSBundle.mainBundle())

252             newCtl.titleString = cell.textLabel?.text

253             viewCtl = newCtl

254         }

255         else if cell.textLabel?.text == "读写应用程序数据"

256         {

257             var newCtl = RWApplicationDataController(nibName: "RWApplicationDataController", bundle: NSBundle.mainBundle())

258             newCtl.titleString = cell.textLabel?.text

259             viewCtl = newCtl

260         }

261         else if cell.textLabel?.text == "多点触摸和手势识别"

262         {

263             var newCtl = MultiTouchController(nibName: "MultiTouchController", bundle: NSBundle.mainBundle())

264             newCtl.titleString = cell.textLabel?.text

265             viewCtl = newCtl

266         }

267         else if cell.textLabel?.text == "多媒体应用"

268         {

269             var newCtl = MultimediaController(nibName: "MultimediaController", bundle: NSBundle.mainBundle())

270             newCtl.titleString = cell.textLabel?.text

271             viewCtl = newCtl

272         }

273         else if cell.textLabel?.text == "动画"

274         {

275             var newCtl = AnimationController(nibName: "AnimationController", bundle: NSBundle.mainBundle())

276             newCtl.titleString = cell.textLabel?.text

277             viewCtl = newCtl

278         }

279         else if cell.textLabel?.text == "网络通信"

280         {

281             var newCtl = NetworkViewController(nibName: "NetworkViewController", bundle: NSBundle.mainBundle())

282             newCtl.titleString = cell.textLabel?.text

283             viewCtl = newCtl

284         }

285         else if cell.textLabel?.text == "检测设备朝向和移动"

286         {

287             var newCtl = DeviceMovingController(nibName: "DeviceMovingController", bundle: NSBundle.mainBundle())

288             newCtl.titleString = cell.textLabel?.text

289             viewCtl = newCtl

290         }

291         else if cell.textLabel?.text == "定位处理与地图"

292         {

293             var newCtl = MapAndLocationController(nibName: "MapAndLocationController", bundle: NSBundle.mainBundle())

294             newCtl.titleString = cell.textLabel?.text

295             viewCtl = newCtl

296         }

297         else if cell.textLabel?.text == "地址簿、短信、邮件"

298         {

299             var newCtl = AddressSMSEmailController(nibName: "AddressSMSEmailController", bundle: NSBundle.mainBundle())

300             newCtl.titleString = cell.textLabel?.text

301             viewCtl = newCtl

302         }

303         else if cell.textLabel?.text == "iOS应用本地化"

304         {

305             var newCtl = LocalizationController(nibName: "LocalizationController", bundle: NSBundle.mainBundle())

306             newCtl.titleString = cell.textLabel?.text

307             viewCtl = newCtl

308         }

309         

310         

311         

312         

313         self.navigationController?.pushViewController(viewCtl, animated: true)

314     }

315     

316     /*

317     // MARK: - Navigation

318 

319     // In a storyboard-based application, you will often want to do a little preparation before navigation

320     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {

321         // Get the new view controller using segue.destinationViewController.

322         // Pass the selected object to the new view controller.

323     }

324     */

325 

326 }

 完整代码:

  1 class RootViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {  

  2       

  3     var tableView : UITableView?  

  4     var items = ["武汉","上海","北京","深圳","广州","重庆","香港","台海","天津"]  

  5     var leftBtn:UIButton?  

  6     var rightButtonItem:UIBarButtonItem?  

  7       

  8     override func viewDidLoad() {  

  9         super.viewDidLoad()  

 10         initView()  

 11         setupRightBarButtonItem()  

 12         setupLeftBarButtonItem()  

 13         self.leftBtn!.userInteractionEnabled = true  

 14   

 15         // Do any additional setup after loading the view.  

 16     }  

 17   

 18     func initView(){  

 19         // 初始化tableView的数据  

 20         self.tableView=UITableView(frame:self.view.frame,style:UITableViewStyle.Plain)  

 21         // 设置tableView的数据源  

 22         self.tableView!.dataSource=self  

 23         // 设置tableView的委托  

 24         self.tableView!.delegate = self  

 25         //  

 26         self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")  

 27         self.view.addSubview(self.tableView!)  

 28          

 29           

 30     }  

 31     //加左边按钮  

 32     func setupLeftBarButtonItem()  

 33     {  

 34         self.leftBtn = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton  

 35         self.leftBtn!.frame = CGRectMake(0,0,50,40)  

 36         self.leftBtn?.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)  

 37         self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal)  

 38         self.leftBtn!.tag = 100  

 39         self.leftBtn!.userInteractionEnabled = false  

 40         self.leftBtn?.addTarget(self, action: "leftBarButtonItemClicked", forControlEvents: UIControlEvents.TouchUpInside)  

 41         var barButtonItem = UIBarButtonItem(customView: self.leftBtn)  

 42         self.navigationItem!.leftBarButtonItem = barButtonItem  

 43     }  

 44     //左边按钮事件  

 45     func leftBarButtonItemClicked()  

 46     {  

 47         println("leftBarButton")  

 48         if (self.leftBtn!.tag == 100)  

 49         {  

 50             self.tableView?.setEditing(true, animated: true)  

 51             self.leftBtn!.tag = 200  

 52             self.leftBtn?.setTitle("Done", forState: UIControlState.Normal)  

 53             //将增加按钮设置不能用  

 54             self.rightButtonItem!.enabled=false  

 55         }  

 56         else  

 57         {  

 58             //恢复增加按钮  

 59              self.rightButtonItem!.enabled=true  

 60             self.tableView?.setEditing(false, animated: true)  

 61             self.leftBtn!.tag = 100  

 62             self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal)  

 63         }  

 64           

 65     }  

 66       

 67     //加右边按钮  

 68     func setupRightBarButtonItem()  

 69     {  

 70          self.rightButtonItem = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, target: self,action: "rightBarButtonItemClicked")  

 71         self.navigationItem!.rightBarButtonItem = self.rightButtonItem  

 72   

 73     }  

 74     //增加事件  

 75     func rightBarButtonItemClicked()  

 76     {  

 77           

 78         var row = self.items.count  

 79         var indexPath = NSIndexPath(forRow:row,inSection:0)  

 80         self.items.append("杭州")  

 81         self.tableView?.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)  

 82       

 83       

 84     }  

 85       

 86       

 87     override func didReceiveMemoryWarning() {  

 88         super.didReceiveMemoryWarning()  

 89         // Dispose of any resources that can be recreated.  

 90     }  

 91       

 92     //总行数  

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

 94         return self.items.count  

 95     }  

 96       

 97     //加载数据  

 98     func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{  

 99   

100         let cell = tableView .dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell  

101         var row=indexPath.row as Int  

102         cell.textLabel.text=self.items[row]  

103         cell.imageView.image = UIImage(named:"green.png")  

104         return cell;  

105   

106     }  

107   

108     //删除一行  

109    func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!){  

110         var index=indexPath.row as Int  

111         self.items.removeAtIndex(index)  

112         self.tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)  

113         NSLog("删除\(indexPath.row)")  

114     }  

115         //选择一行  

116     func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){  

117         let alert = UIAlertView()  

118         alert.title = "提示"  

119         alert.message = "你选择的是\(self.items[indexPath.row])"  

120         alert.addButtonWithTitle("Ok")  

121         alert.show()  

122     }  

123   

124       

125   

126

 

 
 

你可能感兴趣的:(UITableView)