zoukankan      html  css  js  c++  java
  • iOS_lastIndexPath的使用(纯代码做界面)

       在学习对tableview中cell的单选使用时,查阅了很多文章,发现很多代码都不可以直接使用,但是对于一个新手从我自身出发,需要的首先是一个可以运转起来的代码,那么我就把我的代码详细的贴出来。

      示例解析:我的file名为:SJMyGenderViewController用来做对男生女生性别进行判断的,贴图如下:

        请原谅我作为一个新手狗屎一样的界面画风 

        .h文件

         

       .m文件

      定义@property (nonatomic, strong) NSIndexPath *lastIndexPath;  //这个都应该知道在哪里定义吧

       

         -(void)viewDidLoad

      {

        [super viewDidLoad];

        int  lastIndexPath = -1;   //在这里加上这一句

        

        self.view.backgroundColor=[UIColor whiteColor];

        [self.view addSubview:self.customNavigationBar];

        [self.view addSubview:self.tableView];

        [self.view addSubview:self.hud];

    }

       之后都不要管,该定义你的cell 就定义cell,改定义label就定义label,你认为合适的空地加入下列代码就可以了,单独的

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        long newRow = [indexPath row];

        long oldRow = [_lastIndexpath row];

        if (newRow != oldRow)

        {

            UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];

            newCell.accessoryType = UITableViewCellAccessoryCheckmark;

            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndexpath];

            oldCell.accessoryType = UITableViewCellAccessoryNone;

            _lastIndexpath = indexPath;

        }

        [tableView deselectRowAtIndexPath:indexPath animated:YES];

    }

    很多情况下定义@property要加上一个这样的定义:

    - (UITableView *)tableView

    {

        if (!_tableView) {

            _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; // UITableView有两种style,默认是UITableViewStylePlain

            _tableView.dataSource = self;

            _tableView.delegate = self;

        }

        return _tableView;

    }

      但是强调一下这里确实不需要加,有了之前点代码就完全可以成功运行了。

        

      

  • 相关阅读:
    cf B. Sereja and Suffixes
    cf E. Dima and Magic Guitar
    cf D. Dima and Trap Graph
    cf C. Dima and Salad
    最短路径问题(floyd)
    Drainage Ditches(网络流(EK算法))
    图结构练习—BFSDFS—判断可达性(BFS)
    Sorting It All Out(拓扑排序)
    Power Network(最大流(EK算法))
    Labeling Balls(拓扑)
  • 原文地址:https://www.cnblogs.com/lepus/p/4760512.html
Copyright © 2011-2022 走看看