iOS 最全的UITableView的各种使用方法
来源:互联网 发布:网络打字工作 编辑:程序博客网 时间:2024/06/03 16:10
UITableView:1、重用代理 @interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>2、定义 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain]; _tableView.delegate = self; //代理 _tableView.dataSource = self; [self.view addSubview:_tableView]; [_tableView release]; //分割线颜色 _tableView.separatorColor = [UIColor redColor]; //分割线类型 //_tableView.separatorStyle = UITableViewCellSeparatorStyleNone; //行高 _tableView.rowHeight = 100; //背景颜色 _tableView.backgroundColor = [UIColor orangeColor]; //_tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"2_4.jpg"]]; //背景图片 UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; imageView.image = [UIImage imageNamed:@"2_4.jpg"]; _tableView.backgroundView = imageView; [imageView release];3、设置组的行数想要确定哪一组,则要根据section的值来判断- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _dataArray.count;}4、设置组数- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 2;}5、创建行代理********************判定组用:indexPath.section判定行:indexPath.row********************- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:indexPath.row%2==0 ? @"ID1" : @"ID2"]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indexPath.row%2==0 ? @"ID1" : @"ID2"] autorelease]; if (indexPath.row%2==0) { cell.contentView.backgroundColor = [UIColor redColor]; } else { cell.contentView.backgroundColor = [UIColor blueColor]; } } //cell选中效果 cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row]; NSLog(@"%d",indexPath.row); cell.textLabel.textColor = [UIColor whiteColor]; return cell;}6、//组标题 头部- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [NSString stringWithFormat:@"%d",section];}//组标题 尾部- (NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return @"end";}//行高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{// indexPath.section// indexPath.row if (indexPath.section == 0) { return 50; } return 80;}//每一组头部的高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 20;}6、设置选中状态 的闪现: [cell setSelected:NO animated:NO];//选中时 调用的方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = (UITableViewCell *)[tableView viewWithTag:indexPath.section *10 + indexPath.row+1]; // NSLog(@"%@",cell.textLabel.text); cell.textLabel.textColor = [UIColor blueColor]; cell.accessoryType = UITableViewCellAccessoryCheckmark; [cell setSelected:NO animated:NO];}//撤销选中时,调用的方法,可用于取消 accessoryType的状态,文字的颜色改变等- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = (UITableViewCell *)[tableView viewWithTag:indexPath.section *10 + indexPath.row+1]; cell.textLabel.textColor = [UIColor blackColor]; cell.accessoryType = UITableViewCellAccessoryNone;}7、//加组索引,只有超过一行的时候才能发挥作用- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{ return [NSArray arrayWithObjects:@"A", @"B" , @"C", @"D", nil];}8、 编辑//编辑按钮的事件设置- (void)edit{ [_delArray removeAllObjects]; if (_tableView.editing) { [_tableView setEditing:NO animated:YES]; } else { [_tableView setEditing:YES animated:YES]; }}//删除按钮的事件- (void)delButtonClick{ NSMutableIndexSet *index = [NSMutableIndexSet indexSet]; for (NSIndexPath* indexPath in _delArray) { [index addIndex:indexPath.row]; } [_dateArray removeObjectsAtIndexes:index]; [_tableView deleteRowsAtIndexPaths:_delArray withRowAnimation:UITableViewRowAnimationAutomatic]; [_delArray removeAllObjects];}//允许编辑- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}//指定编辑模式,插入,删除- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ //单插入 //return UITableViewCellEditingStyleInsert ; //多选删除 return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;}//触发编辑方法;根据editingStyle来判断时那种类型- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ //删除 if (editingStyle == UITableViewCellEditingStyleDelete) { //先从数组删除 [_dateArray removeObjectAtIndex:indexPath.row]; //再从表格删除 //删除行,可以单行,也可以多行 [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; } if (editingStyle == UITableViewCellEditingStyleInsert) { //先插入数组 [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }}//更改删除按钮上的文本- (NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"删掉我吧";}//编辑时 数据数组的处理//多选删除 选中- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (tableView.editing) { [_delArray addObject:indexPath]; }}//取消选中- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ if (tableView.editing) { [_delArray removeObject:indexPath]; } }9、在AppDelegate.m里面添加UINavigationController,视图控制- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:self.viewController]; self.window.rootViewController = nc; [self.window makeKeyAndVisible]; return YES;}10、Cell的三种创建方法方法1:自己创建 继承UITableViewCell 的类#import <UIKit/UIKit.h>@interface UserCell : UITableViewCell@property (nonatomic, retain) UILabel* userLabel;@property (nonatomic, retain) UIImageView* userView;@end.m文件#import "UserCell.h"@implementation UserCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self makeView]; } return self;}//自建View- (void)makeView{ //60 self.userView = [[[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)] retain]; [self.contentView addSubview:self.userView]; [self.userView release]; self.userLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 10, 200, 20)]; [self.contentView addSubview:self.userLabel]; [self.userLabel release];}//由于上面property中写了retain,这里需要再次释放,- (void)dealloc{// [self.userView release];// [self.userLabel release]; self.userLabel = nil; self.userView = nil; [super dealloc];}@end方法二:用XIB创建1.创建的时候Class的名字写和。h文件一样的名字2.Table View Cell里面的Identifiel属性 设置成ID,和.m文件的索引字符串一样3.在XIB里面只能创建一个CELL,不能由其他孤立的控件,删除的时候要用 delete键删除,不然不够干净4.设置 控件的Tag值5.在.m文件里面- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ID"]; if (cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"UserCell" owner:self options:nil] lastObject]; } UILabel* label = (UILabel*)[cell viewWithTag:20]; UIImageView* imageView = (UIImageView*)[cell viewWithTag:10]; label.text = [_dataArray objectAtIndex:indexPath.row]; imageView.image = [UIImage imageNamed:@"1.png"]; return cell;}方法三:UserCell 和XIB象结合1.修改UserCell.h文件#import <UIKit/UIKit.h>@interface UserCell : UITableViewCell@property (nonatomic, retain) IBOutlet UILabel* userLabel;@property (nonatomic, retain) IBOutlet UIImageView* userView;@end2.在XIB里面 建立空间的索引:按 右键,拖拉线3..m文件- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UserCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ID"]; if (cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"UserCell" owner:self options:nil] lastObject]; } cell.userLabel.text = [_dataArray objectAtIndex:indexPath.row]; cell.userView.image = [UIImage imageNamed:@"1.png"]; return cell;}11、 设置行高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 60;`}行高自适应:- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // 列寬 CGFloat contentWidth = self.tableView.frame.size.width; // 用何種字體進行顯示 UIFont *font = [UIFont systemFontOfSize:13]; // 該行要顯示的內容 NSString *content = [data objectAtIndex:indexPath.row]; // 計算出顯示完內容需要的最小尺寸 CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap]; // 這裏返回需要的高度 return size.height;}//contentWidth一般可以直接设置成屏幕宽度,或者一个定值,这样就简单了//系统是先把全部的行高设置完后,才会进行cell的内容设置,所以在设置行高的时候时无法获取cell的// Customize the appearance of table view cells.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; // 列寬 CGFloat contentWidth = self.tableView.frame.size.width; // 用何種字體進行顯示 UIFont *font = [UIFont systemFontOfSize:13]; // 該行要顯示的內容 NSString *content = [data objectAtIndex:indexPath.row]; // 計算出顯示完內容需要的最小尺寸 CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap]; // 構建顯示行 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0]; // 設置顯示榘形大小 rect.size = size; // 重置列文本區域 cell.textLabel.frame = rect; cell.textLabel.text = content; // 設置自動換行(重要) cell.textLabel.numberOfLines = 0; // 設置顯示字體(一定要和之前計算時使用字體一至) cell.textLabel.font = font; return cell;}12、获取cell的方法 NSIndexPath *indexPath2 = [NSIndexPath indexPathForItem:indexPath.row inSection:0];BookCell *cell = (BookCell *)[_tableView cellForRowAtIndexPath:indexPath2]
来源于:http://www.cnblogs.com/zibuyu/p/3564424.html
0 0
- iOS 最全的UITableView的各种使用方法
- ios--UITableview的基本使用方法
- ios--UITableview的基本使用方法
- 最全的word使用方法
- [iOS]各种定时器--最全的定时器使用
- UITableView 基本使用方法 iOS的listview
- 最全markdown编辑器的使用方法
- uitableview 的一些使用方法
- UITableView的使用方法
- UITableView的基本使用方法
- iOS 最全ReactiveCocoa原理及使用方法
- ios uitableview出现各种意外状况的原因
- IOS的UITableView显示最底部的cell
- c# 反射的使用方法,不知道是不是最全的
- c# 反射的使用方法,不知道是不是最全的
- 【Demo】IOS使用UItableView实现下拉菜单组件(UITableView的使用方法)
- IOS使用UItableView实现下拉菜单组件(UITableView的使用方法)
- iOS开源库--最全的整理
- 网页天气预报iframe引用
- jquery mobile 动态修改select选择框
- Handler.obtainMessage()的认识和理解
- npm intall error MSB3428 on windows7
- co01/co02时通过增强 EXIT_SAPLCOSD_001修改组件的默认库存地点
- iOS 最全的UITableView的各种使用方法
- JavaScript模块-加载模块require.js
- mysql数据库乱码
- 汇编语言程序设计
- Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
- iOS 输入框过滤emoji表情
- Deep Learning 初探(五)
- 怎样退出终止App
- codeblock常用快捷键