iOS UITableView的一些方法

项目中用到的一些tabview 问题及对应方法:

一.tableview

1.下划线左对齐

//步骤一:(加在 viewdidload方法中)
    if([tabView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [tabView setSeparatorInset:UIEdgeInsetsZero];
    }
    if
        ([tabView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [tabView setLayoutMargins:UIEdgeInsetsZero];
    }
//步骤二:修改分割线方法 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } }

 

二.cell

1.自定义cell获取选中的cell

NSIndexPath *indexPath = [tabView indexPathForSelectedRow];
UITableViewCell
*cell = [tabView cellForRowAtIndexPath:indexPath]; cell.myLable.text= @"abc";

2.下拉列表单选(文字选中变色等)

//选中变色
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"选中某项变色");
    SortTableViewCell *cell = [tabView cellForRowAtIndexPath:indexPath];
    cell.sortLable.textColor = RGBCOLOR(51, 153, 204);
}

//再次点击取消
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"取消某项变色");
    SortTableViewCell *cell = [tabView cellForRowAtIndexPath:indexPath];
    cell.sortLable.textColor = [UIColor blackColor];
}

补充:如果还要默认选中第一项同时还得触发触发didselect方法  用以下方法

NSIndexPath *firstPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tabView selectRowAtIndexPath:firstPath animated:YES scrollPosition:UITableViewScrollPositionTop];
 if ([tabView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
      [tabView.delegate tableView:tabView didSelectRowAtIndexPath:firstPath];
}

 

3. cell的单选和多选

项目需要做试题的单选和课程下载的多选,首先想到用tabview的didselect配合didDeselect两个协议的方法(参考:http://www.2cto.com/kf/201412/364359.html),后来没用;

因为试题ABCD四个选项的单选题样式 所以在cell中加上button按钮解决;而下载是有分区的多选样式就根据下载图片的前后位置不同分别采取了cell加button的方法和系统自带的编辑方法

4.取消多余空白cell

tabView.tableFooterView = [[UIView alloc] init];//去掉空白cell

5.点击效果

(1).自定义点击时的背景色

UIView *view = [[UIView alloc]init];
view.backgroundColor =  RGBCOLOR(229, 246, 254);
cell.selectedBackgroundView = view;

(2).点击后返回cell的颜色为无色

cell.selectionStyle = UITableViewCellSelectionStyleNone;

(3).点击时有颜色变化 返回cell的颜色为无色(配合自定义cell选中背景色)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

 

 

 

三.混合嵌套

1.tableview二级菜单

 

posted @ 2016-06-27 10:49  wusang  阅读(456)  评论(0编辑  收藏  举报