chenzhao

  • java
  • iOS
  • IT
知识积累
不积跬步无以至千里
  1. 首页
  2. iOS
  3. 正文

UITableView 一些点点滴滴

2017年 8月 5日 76点热度 0人点赞 0条评论

From: http://www.jianshu.com/p/bb21b4d45a8d

UITableViewCell的选中时的颜色设置

1.系统默认的颜色设置

//无色
cell.selectionStyle = UITableViewCellSelectionStyleNone;

//蓝色
cell.selectionStyle = UITableViewCellSelectionStyleBlue;

//灰色
cell.selectionStyle = UITableViewCellSelectionStyleGray;

2.自定义颜色和背景设置

改变UITableViewCell选中时背景色:

UIColor *color = [[UIColoralloc]initWithRed:0.0 green:0.0 blue:0.0 alpha:1];//通过RGB来定义自己的颜色

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];//这句不可省略
cell.selectedBackgroundView.backgroundColor = [UIColor xxColor];

3.自定义UITableViewCell选中时背景

cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellart.png"]] ; 
 //还有字体颜色 
cell.textLabel.highlightedTextColor = [UIColor xxxcolor];  [cell.textLabel setTextColor:color];//设置cell的字体的颜色

4.设置tableViewCell间的分割线的颜色

[theTableView setSeparatorColor:[UIColor xxColor]];

5、设置cell中字体的颜色

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(indexPath.row == 0)
  {
    cell.textLabel.textColor = ...;
    cell.textLabel.highlightedTextColor = ...;
  }
}

// tableviewcell 选中图标
override func layoutSubviews() {
        self.selectedBackgroundView?.frame = self.bounds
        
        super.layoutSubviews()
        if #available(iOS 8 ,  *){
            // ios7 没效果 第一次也不出现,还是得子定义
            for control in self.subviews{
                if control.isMember(of: NSClassFromString("UITableViewCellEditControl")!){
                    for  v in control.subviews{
                        if v.isKind(of: UIImageView.classForCoder()){
                            //var img = v as! UIImageView
                            // v = UIImageView(image: UIImage(named: "activity_close"))
                            if self.isSelected{
                                
                                (v as! UIImageView).image = UIImage(named: "bjzl_gx")//2x 为40  v2_4_btn_set_selected
                            }else{
                                //(v as! UIImageView).image = UIImage(named: "activity_new")//必须原先的图 因为效果不能一直有
                                
                            }
                        }
                    }
                }
            }
        }else{
            for view in self.subviews{
                if view.isMember(of: NSClassFromString("UITableViewCellScrollView")!){
                    printLog("views \(view.subviews)")
                    let views2 = view.subviews
                    for control in views2{
                        if control.isMember(of: NSClassFromString("UITableViewCellEditControl")!){
                            for v in control.subviews{
                                if v.isKind(of: UIImageView.classForCoder()){
                                    let img = v as! UIImageView
                                    if self.isSelected{
                                        img.image = UIImage(named: "bjzl_gx")
                                    }else{
                                        //img.image = UIImage(named: "activity_new")//必须原先的图 因为效果不能一直有
                                    }
                                }
                            }
                            
                        }
                    }
                    
                }
            }
        }
        
        
    }

----- 遇到一个坑爹的问题-- 顶部留白20像素 ----

1 不是 automaticallyAdjustsScrollViewInsets 问题 已经设置false
2 不是ios 11 的问题 已经设置不自动计算
tab

self.estimatedRowHeight = 0
self.estimatedSectionFooterHeight = 0;
self.estimatedSectionHeaderHeight = 0;

3 最后, 原因 UITableViewWrapperView 和 UItableView frame 不一致造成的,
据说是 navigationBar.alpha 不为1 会出现,自动下移, 在布局后 重置下内容位置

  override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews();
        self.tableView.contentInset = UIEdgeInsets.zero;
    }

滑动到顶部 或者底部

- (void)scrollToBottom
{
    CGFloat yOffset = 0; //设置要滚动的位置 0最顶部 CGFLOAT_MAX最底部
    if (self.tableView.contentSize.height > self.tableView.bounds.size.height) {
        yOffset = self.tableView.contentSize.height - self.tableView.bounds.size.height;
    }
    [self.tableView setContentOffset:CGPointMake(0, yOffset) animated:NO];
}
// 可能会有bug ,用下面的方式


   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.messageModelArray.count-1 inSection:0];
    
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

标签: 暂无
最后更新:2022年 11月 11日

陈昭

IT 程序员

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

COPYRIGHT © 2022 chenzhao. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang