IOS开发之UITableView

UITableView就相当于android中的listview,在这里先介绍一个简单的UItableView的用法,因为我也是刚学。而且也遇到了一些问题。直接上代码吧

这是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)section

2、图片不能正常显示的原因

cell.imageView.opaque=YES;//must add this sentence,or the image will gone

这个opaque属性要设为YES,默认是NO

3、图片无需写路径,但是后缀名一定要写,比如.jpg,.png

时间: 2024-07-30 11:50:45

IOS开发之UITableView的相关文章

iOS开发之UITableView全面解析

在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是UITableView.当然它的广泛使用自然离不开它强大的功能,今天这篇文章将针对UITableView重点展开讨论.今天的主要内容包括: 1.基本介绍 2.数据源 3.代理 4.性能优化 5.UITableViewCell 6.常用操作 7.UITableViewController 8.MVC模式   基本介绍 UITableView有两种风

iOS开发之UITableView的使用

这一篇记录的是iOS开发中UITableView的使用,iOS中的UITableView跟Android中的ListView特别相似,以下用一个Demo来说明: 1.Xcode中新建projectTestSimpleTableViewproject 2.在Main.storyboard中拖入一个UITableView控件 3.在ViewController.h文件里,实现UITableViewDelegate和UITableViewDataSource协议 这里须要说下的是.为了给UITable

IOS开发之UITableView使用大全。

前言: UITableView是ios开发中最常用的控件之一,几乎所有的应用都要用到,tableview继承UIScrollView,因此它不仅可以显示多行数据,而且具有scrollview的一些操作功能,比如滑动,自动偏移等等,因此非常强大. 而且tableview采用了数据源模式,因此只需要更改它的数据源,即可实现tableview显示数据的变化,而且tableviewcell还具有复用性. 所以tableview是一个在显示大量数据时及其好的选择. 1. tableview采用的是代理模式

iOS开发之UITableView使用总结

什么是UITableView 在众多移动应用中,能看到各式各样的表格数据 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳 UITableView的两种样式 UITableViewStylePlain UITableViewStyleGrouped tableView展示数据的过程 1.调用数据源的下面方法得知一共有多少组数据 - (NSInteger)numberOfSections

iOS开发之UITableView及cell重用

1.UITanleview有的两种风格 一种是Plain,一种是Grouped,可以从这里设置风格: 他们样式分别如下: Plain: Grouped: 2.tableView展示数据的过程: (1)首先,控制器要遵守UITableViewDataSource协议 @interface ViewController () <UITableViewDataSource> (2)调用数据源的下面方法得知一共有多少组数据 - (NSInteger)numberOfSectionsInTableVie

IOS开发之UITableView的奇技

作者:Biaoac age:保密 sex:直男 性格:低调沉稳,乖张内涵 博客背景:之前一直在使用UITableView,但是一直都只是初识,后来在不断的使用中找到了很多之前没有在意的东西,遂整理出来,当然,有很多还是看别人的博客中提到的点,我把他重踩一遍: 1.点击的时候显示选中状态,但状态一直村在,必须在点击下一个的时候取消选中状态 点击cell的时候调用 - (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(N

4、iOS 开发之 UITableView

一.UITableView的创建 表格控件在创建时必须指定样式,只能使用以下实例化方法 [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; UITableView的两种样式 1> UITableViewStylePlain 2> UITableViewStyleGrouped 2.UITableView的常见属性 // 头部视图(广告) @property (nonatomic,

iOS开发之UITableView的滚动优化以及隐藏特性的使用

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源码下载:点我传送 游戏官方下载:http://dwz.cn/RwTjl 游戏视频预览:http://dwz.cn/RzHHd 游戏开发博客:http://dwz.cn/RzJzI 游戏源码传送:http://dwz.cn/Nret1 影响 UITableView 滚动的流畅性的原因 1. 在代理方法中做了过多的计算占用了 UI

李洪强iOS开发之RunLoop的原理和核心机制

李洪强iOS开发之RunLoop的原理和核心机制 搞iOS之后一直没有深入研究过RunLoop,非常的惭愧.刚好前一阵子负责性能优化项目,需要利用RunLoop做性能优化和性能检测,趁着这个机会深入研究了RunLoop的原理和特性. RunLoop的定义 当有持续的异步任务需求时,我们会创建一个独立的生命周期可控的线程.RunLoop就是控制线程生命周期并接收事件进行处理的机制. RunLoop是iOS事件响应与任务处理最核心的机制,它贯穿iOS整个系统. Foundation: NSRunLo