第一次在简书上写文章

匆匆岁月

学习iOS已经很久了,总想记录点什么,总被我的借口一次一次的给挡在门外了,想着没事看个电视剧;想着没事对着全民K歌唱唱歌,想着怎么做一顿美味可口的饭菜;感觉自己是一个不称职的码农,有种危机感,在此今天鼓起勇气写第一篇文章,有个开始,然后在开始的道路上继续前进,内心不停的呐喊加油。

计划

  • 未来两个月学习swift3.0
  • 多线程
    什么通讯聊天、流媒体、socket等等这些也都在学习的范围内,对于表用的最多,但是他的协议方法的执行顺序还是值得深究下的。

实现光标随着点击的cell移动

今天了解到表的selectedBackgroundView以及代理方法在此给大家分享下,功能就是点击单元格实现移动光条:

效果图:

42C527D6-90B0-4858-8EE5-6C9005EC3226.png
32358BF9-7A7D-4FFD-8D70-46CB4F129D2B.png

代码实现:

  • cellForRowAtIndexPath方法:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSLog(@"11111111111111");

    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        cell.backgroundColor =[UIColor clearColor];
        UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        selectedBackgroundView.backgroundColor = [UIColor whiteColor];
        cell.selectedBackgroundView = selectedBackgroundView;   
        // 左侧示意条
        UIView *liner = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 2, 40)];
        liner.backgroundColor = [UIColor orangeColor];
        [selectedBackgroundView addSubview:liner];
    }
    cell.textLabel.text=[NSString stringWithFormat:@"第%ld个",indexPath.row];
        return cell; 
}
  • 代理方法:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    
    NSLog(@"22222222222222");
    if (_isRelate==YES) {
            [tableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    }
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0){
    NSLog(@"33333333333333");
    _isRelate=NO;
}

当然也有很多其他的实现方式就不多做介绍了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容