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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机卡号不知道怎么办 在手机上怎么办手机号 办手机卡被骗了怎么办 大王卡注销余额怎么办 注销大王卡话费怎么办 手机卡里还有话费怎么办 多余的话费要怎么办 联通流量欠费500怎么办 手机卡没了怎么办呢 联通显示无服务怎么办 大王卡停用了怎么办 手机卡中 剩余话费怎么办 小米被流量限制怎么办 支付宝禁止联网怎么办 手机号登录不上怎么办 电信手机号无法打电话怎么办 新手机号无法注册怎么办 电信手机怎么办流量包 qq华夏到监牢怎么办 炫舞被骗2900怎么办 qq好友申诉后怎么办 无好友辅助申请怎么办 辅助申诉成功后怎么办 qq绑不了手机号怎么办 新办号码有微信怎么办 两手机互换号后微信怎么办 手机申请不了qq怎么办 快手账号保护了怎么办 qq号过期登不上怎么办 xp 登陆界面不见了怎么办 手机号qq 别人登录怎么办 qq号申请频繁怎么办 qq号实名验证怎么办 陌陌没法注册怎么办 腾讯q币充值错误怎么办 手机号码注册微信号怎么办 q币充错了号怎么办 微信超额度提现都不可以怎么办 微信发红包转账限额怎么办 银行卡没设密码怎么办 宜人贷逾期一天怎么办