IOS开发之UITableView
2015-07-23 10:41
253 查看
UITableView就相当于android中的listview,在这里先介绍一个简单的UItableView的用法,因为我也是刚学。而且也遇到了一些问题。直接上代码吧
这是ViewController.h
1、两个必须实现的协议方法,相当于java的接口的抽象方法
3、图片无需写路径,但是后缀名一定要写,比如.jpg,.png
本文出自 “灵哥笔记” 博客,请务必保留此出处http://kinghacker.blog.51cto.com/7767090/1677432
这是ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property (nonatomic,retain) NSArray * fileList; @property (nonatomic,retain) UITableView * tableView; @end这是ViewController.m文件
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSBundle * bundle = [NSBundle mainBundle]; NSString * plistPath = [bundle pathForResource:@"infoHeaderAndTitleAndDetile" ofType:@"plist"]; self.fileList=[[NSArray alloc] initWithContentsOfFile:plistPath]; UITableView * tView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped]; tView.delegate=self; tView.dataSource=self; _tableView=tView; [self.view addSubview:_tableView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellWithIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellWithIdentifier]; } NSUInteger row = [indexPath row]; NSDictionary *rowDict = [self.fileList objectAtIndex:row]; cell.textLabel.text = [rowDict objectForKey :@"title"]; cell.imageView.opaque=YES;//must add this sentence,or the image will gone cell.imageView.image = [UIImage imageNamed:[[rowDict objectForKey :@"header"] stringByAppendingString:@".jpg"]];//image must have the .jpg or other NSLog(@"%@",cell.imageView); cell.detailTextLabel.text = [rowDict objectForKey :@"detail"]; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; return cell; } - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { return [self.fileList count]; } @end需要注意的地方有三点,也是我遇到的问题
1、两个必须实现的协议方法,相当于java的接口的抽象方法
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath和
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section2、图片不能正常显示的原因
cell.imageView.opaque=YES;//must add this sentence,or the image will gone这个opaque属性要设为YES,默认是NO
3、图片无需写路径,但是后缀名一定要写,比如.jpg,.png
本文出自 “灵哥笔记” 博客,请务必保留此出处http://kinghacker.blog.51cto.com/7767090/1677432
相关文章推荐
- 如何修改序列(Sequence)的初始值(START WITH)
- 动态计算UITableViewCell高度
- /var/spool/clientmqueue文件多导致磁盘满
- java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.ValueStack
- 【从零开始学NGUI 】 (五)PopupList
- make 2>&1 | tee build.log 求解释
- [leetcode] Longest Consecutive Sequence
- 【Guitar Pro 6 中文破解版】完整安装教程+下载地址
- soapui 不同版本之间切换excel
- 2.RequestParam、RequestHeader、CookieValue注解
- hdu5297 Y sequence(容斥原理+迭代)
- Hibernate Query Language
- 自定义系统控件的外观:UIApearance
- 小胖说事34-----iOS UIImageView 的contentMode属性应用
- [转载]Integer.parseInt()和这个Integer.valueOf()的详解
- Win10准正式版Build 10240多国语言包下载(含111种)
- 优化UITableViewCell高度计算的那些事
- 【从零开始学NGUI 】 (四)Slider
- codeforces(559B)--B. Equivalent Strings(暴搜 或 最小表示法)
- Codeforces Round #313 (Div. 2)——D递归,stirng——Equivalent Strings