UITableView 總結

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]
相關文章
相關標籤/搜索