• 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;

    }

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

        

      

  • 相关阅读:
    Six steps to create google map in the HTML5
    Vocabularies on vegetable, fruit, meat etc.
    常用的Windows命令
    sqlhelper
    素材
    sql sever 跨库查询
    在annotation中却会有ERROR: Duplicate entry
    一份Java学习路线图
    Java算法实例集合(2)
    Java编程规范实践
  • 原文地址:https://www.cnblogs.com/lepus/p/4760512.html
Copyright © 2020-2023  润新知