UITableView的常用方法与示例
来源:互联网 发布:spine3.4 破解版 mac 编辑:程序博客网 时间:2024/06/06 23:33
实例方法
dequeueReusableCellWithIdentifier:
初始化一个指定重用标识符的UITableCell对象
初始化一个指定重用标识符的UITableCell对象
两个协议
UITableViewDataSource
tableView:numberOfRowsInSection: (required)
行数在指定分区中
tableView:cellForRowAtIndexPath: (required)
每个Cell显示的具体内容
numberOfSectionsInTableView:
分区的个数
tableView:titleForHeaderInSection:
分区的标题
tableView:canEditRowAtIndexPath:
能否编辑
行数在指定分区中
tableView:cellForRowAtIndexPath: (required)
每个Cell显示的具体内容
numberOfSectionsInTableView:
分区的个数
tableView:titleForHeaderInSection:
分区的标题
tableView:canEditRowAtIndexPath:
能否编辑
UITableViewDelegate
tableView:didSelectRowAtIndexPath:
点击cell时执行的委托方法
点击cell时执行的委托方法
简单的Demo
效果图
点击细节按钮将显示一个AlertView。如果想显示细节信息,亦可以用Master-Detail模板进行创建项目,里面提供好了很多现成的方法,更加方便和快捷.
初始准备
实现协议,增加成员
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>{ NSArray *_redFlowers; NSArray *_blueFlowers;}
预定义数字
#define kSectionCount 2#define kRedSection 0#define kBlueSection 1
初始化成员
- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. _redFlowers = @[@"Gerbera", @"Peony", @"Rose", @"poppy"]; _blueFlowers = @[@"Hyacinth", @"Hydrangea", @"Sea Holly", @"Phlox", @"Iris"];}
实现委托方法
分区及分区标题,分区中的行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return kSectionCount;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ switch (section) { case kRedSection: return [_redFlowers count]; case kBlueSection: return [_blueFlowers count]; default: return 0; }}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ switch (section) { case kRedSection: return @"Red"; case kBlueSection: return @"Blue"; default: return @"Unknown"; }}
显示的Cell信息
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"flowerCell"]; switch (indexPath.section) { case kRedSection: cell.textLabel.text = _redFlowers[indexPath.row]; break; case kBlueSection: cell.textLabel.text = _blueFlowers[indexPath.row]; break; default: cell.textLabel.text = @"Unknown"; } UIImage *flowerImage; flowerImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@", cell.textLabel.text, @".png"]]; cell.imageView.image = flowerImage; return cell;}
这里重用标识符可以是自定义的Cell,也可以是在xib中设置好的系统带的样式,然后在属性标识板中给它设置标识符。
图片是自己个人加入到项目中的。
然后实现选择时弹出AlertView的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UIAlertView *showSelection; NSString *messageString; switch (indexPath.section) { case kRedSection: messageString = [NSString stringWithFormat:@"You chose the red flower - %@", _redFlowers[indexPath.row]]; break; case kBlueSection: messageString = [NSString stringWithFormat:@"You chose the blue flower - %@", _blueFlowers[indexPath.row]]; break; default: messageString = @"Unknown"; break; } showSelection = [[UIAlertView alloc] initWithTitle:@"Flower Selected" message:messageString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [showSelection show];}
- UITableView的常用方法与示例
- UITableView的分组示例及常用方法
- UITableView的常用方法
- UITableView 常用的几个方法
- UITableView的常用代理方法
- UITableView的常用代理方法
- UITableView的一些常用方法
- UITableView的常用代理方法
- UITableView的常用代理方法
- UITableView 常用的委托方法:
- UIScrollView & UITableView 一些常用方法与属性
- UItableView编辑的使用edit, UItableView的一些常用方法
- UITableView的常用方法和属性
- iOS UITableView的一些常用属性,方法
- UITableView 常用方法总结
- 3.UItableView常用方法
- UITableView 常用方法列表
- UITableView中常用方法
- 让代码飞一会儿,超级有用的CSS编码工具集
- 强烈推荐!”计算机中的数学“系列视频
- 第四周13/03/31
- 关于多线程学习总结(四) 锁
- [每日一题] OCP1z0-047 :2013-07-19 Rules of Precedence――括号的使用.................8
- UITableView的常用方法与示例
- 爱上IFTTT无法自拔 --[建立你的网络服务秘书]
- 使用堆栈实现括号的匹配
- TPS和QPS的区别
- android开发中常用到的设置
- 将json合适字符串转换为json对象
- 做个文艺的IT人员——今日工作总结-2013.07.18
- QT入门笔记--信号和槽(附简单的C++类实现代码)
- [联通版] [ROM][9502][联通原版][I9502ZNUAMDH][ZNUAMDI][ZNUAME3]五件套