当前位置: 代码迷 >> Iphone >> iPhone开发之UITableView容易应用
  详细解决方案

iPhone开发之UITableView容易应用

热度:18   发布时间:2016-04-25 05:53:40.0
iPhone开发之UITableView简单应用

1.单击菜单File->New->Proget.....新建一个项目:

???

?? 2.选择Single View Application模板,单击Next:

???

???3.将项目命名为Simple Table,单击Next->Create,新建项目:

???

???4.单击ViewController.xib文件,在View视图上添加控件Table View:

??

? 5.为控件Table View添加数据、方法委托:

??

?

? 6.单击ViewController.h头文件,为ViewController添加数据成员:

[plain] view plaincopy
  1. #import?<UIKit/UIKit.h>??
  2. ??
  3. @interface?ViewController?:?UIViewController<UITableViewDelegate,UITableViewDataSource>??
  4. {??
  5. ???NSArray?*list;??
  6. }??
  7. @property(nonatomic,retain)NSArray?*list;??
  8. ??
  9. @end??


? 7.单击ViewController.m文件,为ViewController添加数据和协议方法:

[plain] view plaincopy
  1. #import?"ViewController.h"??
  2. ??
  3. @interface?ViewController?()??
  4. ??
  5. @end??
  6. ??
  7. @implementation?ViewController??
  8. @synthesize?list;??
  9. ??
  10. -?(void)viewDidLoad??
  11. {??
  12. ????[super?viewDidLoad];??
  13. ????//?Do?any?additional?setup?after?loading?the?view,?typically?from?a?nib.??
  14. ????NSArray?*array?=?[[NSArray?alloc]initWithObjects:@"广东",@"湖南",@"北京",@"上海",@"香港",@"澳门",?nil];??
  15. ????self.list?=?array;??
  16. ????[array?release];??
  17. }??
  18. ??
  19. -?(void)viewDidUnload??
  20. {??
  21. ????[super?viewDidUnload];??
  22. ????//?Release?any?retained?subviews?of?the?main?view.??
  23. ????self.list?=?nil;??
  24. }??
  25. ??
  26. -?(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation??
  27. {??
  28. ????return?(interfaceOrientation?!=?UIInterfaceOrientationPortraitUpsideDown);??
  29. }??
  30. ??
  31. -(void)dealloc??
  32. {??
  33. ????[super?dealloc];??
  34. ????[list?release];??
  35. }??
  36. ??
  37. #pragma?mark?-??
  38. #pragma?mark?Table?Data?Source?Methods??
  39. ??
  40. -(NSInteger)tableView:(UITableView?*)tableView?numberOfRowsInSection:(NSInteger)section??
  41. {??
  42. ????return?[list?count];??
  43. }??
  44. ??
  45. -(UITableViewCell?*)tableView:(UITableView?*)tableView?cellForRowAtIndexPath:(NSIndexPath?*)indexPath??
  46. {??
  47. ????static?NSString?*identfier=@"placetable";??
  48. ????UITableViewCell?*cell?=?[tableView?dequeueReusableCellWithIdentifier:identfier];??
  49. ????if(cell==nil)??
  50. ????{??
  51. ????????cell?=?[[UITableViewCell?alloc]initWithStyle:UITableViewCellStyleDefault?reuseIdentifier:identfier];??
  52. ????}??
  53. ????NSInteger?row?=?[indexPath?row];??
  54. ????cell.textLabel.text?=?[list?objectAtIndex:row];??
  55. ????return?cell;??
  56. }??
  57. ??
  58. -(void)tableView:(UITableView?*)tableView?didSelectRowAtIndexPath:(NSIndexPath?*)indexPath??
  59. {??
  60. ????NSInteger?row?=?[indexPath?row];??
  61. ????NSString?*message?=?[list?objectAtIndex:row];??
  62. ????UIAlertView?*alert?=?[[UIAlertView?alloc]initWithTitle:@" 你选择:"?message:message?delegate:self?cancelButtonTitle:@"确 定"?otherButtonTitles:nil];??
  63. ????[alert?show];??
  64. ????[alert?release];??
  65. }??
  66. ??
  67. @end??

? 8.现在可以单击运行按钮,测试程序,单击其中的选项“广东”,效果如下:

??

  相关解决方案