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; @end
2.在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]