星空网 > 软件开发 > ASP.net

iOS_lastIndexPath的使用(纯代码做界面)

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

  示例解析:我的file名为:SJMyGenderViewController用来做对男生女生性别进行判断的,贴图如下:iOS_lastIndexPath的使用(纯代码做界面)

 

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

    .h文件

     iOS_lastIndexPath的使用(纯代码做界面)

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

}

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

 

    

 

 

 

 

 

 

 

  




原标题:iOS_lastIndexPath的使用(纯代码做界面)

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流