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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。