您的位置:首页 > 产品设计 > UI/UE

UITableView常用方法

2015-11-05 22:16 344 查看
1遵守代理协议
2.设置代理
self.tableView.delegate = self;
3 实现代理方法

// 3.设置分割线的样式


// self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


// 4.设置分割线的颜色


self.tableView.separatorColor = [UIColor blueColor];



/**
*  返回多少组
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 1;
}
/**
*  返回多少行
*/

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return  self.heros.count;
}
//返回怎样的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    1.创建cell
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
//    2.设置数据
HeroModel *hero = self.heros[indexPath.row];
//     2.1设置名称
cell.textLabel.text = hero.name;
//     2.2设置图片

cell.imageView.image = [UIImage imageNamed: hero.icon];
//     2.3设置子标题
cell.detailTextLabel.text = hero.intro;
//     2.4设置指示器
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//     2.4可以用来设置右边显示的视图指示器(必须要设置位置)
UISwitch *switch1 = [[UISwitch alloc]init];

UIView *redView = [[UIView alloc]init];
redView.backgroundColor = [UIColor redColor];

cell.accessoryView = redView;
return cell;
}
//可以确定每一cell用不同的高度
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//

// 在这个方法中确定高度.这个方法会在返回怎样的cell方法之前调用

//    if (indexPath.row == 0) {
//        return 100;
//    }else{
//
//        return 60;
//    }
//
//}


隐藏状态栏

- (BOOL)prefersStatusBarHidden{

return YES;
}


/**
*  表示编辑模式,当我们想要进行cell的插入和删除操作的时候就会调用这个方法
*
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle ==  UITableViewCellEditingStyleDelete ) {
//        1.修改模型(删除模型)
HeroModel *hero = self.heros[indexPath.row];
[self.heros removeObject:hero];
//        2.刷新数据

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}else if (editingStyle == UITableViewCellEditingStyleInsert){

//        1.修改模型(插入模型)
HeroModel *hero = [[HeroModel alloc]init];
hero.name = @"大家都很精神";
[self.heros insertObject:hero atIndex:indexPath.row];
//        2.刷新
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}

}
/**
*  表示修改编辑模式
*
*/

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleInsert;

}


/**
*  表示能够让弹出粘贴面板
*
*/
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{

return YES;
}
//表示能够执行复制粘贴剪切的方法
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{

return YES;
}

/**
*  /
*
表示执行action(复制,粘贴,拷贝)方法

*/
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{

//     1.取得得模型
HeroModel *hero = self.heros[indexPath.row];
//     2.1给粘贴面板赋值

if (action == @selector(copy:)) {
[UIPasteboard generalPasteboard].string = hero.icon;
}
//     2.2如果是粘贴方法,那就修改模型,然后刷新数据(也可以采用局部刷新)
if (action == @selector(paste:)) {

hero.icon = [UIPasteboard generalPasteboard].string;

[self.tableView reloadData];
}

}


// 3.滚动到最后的视图

NSIndexPath *indexBtnPath = [NSIndexPath indexPathForRow:self.tgs.count-1 inSection:0];

[self.tableView scrollToRowAtIndexPath:indexBtnPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: