iOS随笔小记--tableview
1>在tableview代理方法以外获取点击的cell
NSIndexPath *indexPath=[goodsDetailsTableView indexPathForSelectedRow];
GoodsDetailsTableViewCell *cell=[goodsDetailsTableView cellForRowAtIndexPath:indexPath];
2>在tableview代理方法以外获取点击的index
UITableViewCell *cell=(UserEvaluationTableViewCell *)btn.superview.superview;
NSIndexPath *indexPath=[userTableView indexPathForCell:cell];
这样可以取到。。。
3>分割线
分割线顶头显示
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:UIEdgeInsetsZero];
}
//Prevent the cell from inheriting the Table View's margin settings(防止单元格继承表视图的边缘设置)
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
[cell setPreservesSuperviewLayoutMargins:NO];
}
//Explictly set your cell's layout margins(设置你的cell的布局空间)
if ([cell respondsToSelector:@selector(setLayoutMargins:)]){
[cell setLayoutMargins:UIEdgeInsetsZero];
}
//去掉某个分割线
if(tableView==_historyWordTableView){
if((_historyWordDataArray.count > 1)&&(indexPath.row == _historyWordDataArray.count-1)){
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, WIDHT);
}
}
4、实现单选
定义一个 NSIndexPath * lastPath;
在cellForRowAtIndexPath方法里面,设置lastPath = indexPath;
在didSelectRowAtIndexPath选中某个cell的方法里面
if (lastPath == indexPath) {
return;
}
UITableViewCell * lastCell = [self.homeTableView cellForRowAtIndexPath:lastPath];
lastCell.accessoryType = UITableViewCellAccessoryNone;
NSIndexPath *indexPath=[self.homeTableView indexPathForSelectedRow];
UITableViewCell *homecell=[self.homeTableView cellForRowAtIndexPath:indexPath];
homecell.accessoryType = UITableViewCellAccessoryCheckmark;
lastPath = indexPath;