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;

    }

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

        

      

  • 相关阅读:
    TP5常用函数总结1
    swiper 自定义的一些需求
    jQuery 点击元素以外任意地方隐藏该元素
    fastadmin中编辑时的fieldlist选项类型,如何跟数据库里的保持一致,并且显示匹配的样式
    leetcode——63. 不同路径 II
    leetcode——62.不同路径
    数组标签结束,下一个标签,动态规划
    leetcode——48. 旋转图像
    leetcode——45. 跳跃游戏 II
    leetcode——41. 缺失的第一个正数
  • 原文地址:https://www.cnblogs.com/lepus/p/4760512.html
Copyright © 2011-2022 走看看