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

UITableView使用小结(滚动到顶部、获取cell、cell选中状态、刷新cell或者section)

2016-01-08 14:34 996 查看
1.让tableview
滚动到顶部 一句话搞定

[_tableViewsetContentOffset:CGPointMake(0,0)animated:NO];


2.获取tableView的cell

- (UITableViewCell *)cellAtIndexRow:(NSInteger)row andAtSection:(NSInteger) section
{
UITableViewCell * cell = (UITableViewCell *)[_tableViewcellForRowAtIndexPath:[NSIndexPathindexPathForRow:row inSection:section]];
return cell;
}


3.设置全部cell选中状态

- (void)buttonClick:(UIButton *)button
{
//设置全部cell选中状态
for(NSIndexPath *cellindexin [_tableViewindexPathsForVisibleRows])
{
//根据indexpath获取cell
UITableViewCell *cell = [_tableViewcellForRowAtIndexPath:cellindex];
if(_isAllSelect)
{
cell.selected = YES;
}
else
{
cell.selected = NO;
}
}
}


4.UITableview 刷新某一个cell 或 section

- (void)refreshSectionOrCell
{
//一个section刷新
NSIndexSet *indexSet=[[NSIndexSetalloc]initWithIndex:2];
[_tableViewreloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

//一个cell刷新
NSIndexPath *indexPath=[NSIndexPathindexPathForRow:3inSection:0];
[_tableViewreloadRowsAtIndexPaths:[NSArrayarrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationNone];
}


5.改变Tableview的cell的选中背景颜色

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = [UIColor redColor];


6.TableviewCell默认选中某一行

// 默认选中第一行
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
// 实现了选中第一行的方法
[self tableView:_mainIndustryTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
//例如:
// 默认下选中状态
- (void)customAtIndex:(UITableView *)tableView
{
// 默认选中第一行
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
if ([tableView isEqual:_mainIndustryTableView]) {
[self tableView:tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: