• 解决TalbleView头部或底部子控件不显示问题


    在自定义cell头部控件UITableViewHeaderFooterView(和自定义cell的方法几乎一样)时,出现了头部控件子控件不显示的问题。

    注意和自定义cell的区别。

    .h文件

     1 #import <UIKit/UIKit.h>
     2 #import "CHModleGroup.h"
     3 @interface HeaderView : UITableViewHeaderFooterView
     4 @property (nonatomic, weak) UILabel *count;
     5 @property (nonatomic, weak) UIButton *name;
     6 + (instancetype)headerViewWithTableView:(UITableView *)tableView;
     7 
     8 @property (nonatomic, strong) CHModleGroup *group;
     9 
    10 @end

    .m文件

     1 #import "HeaderView.h"
     2 
     3 @implementation HeaderView
     4 + (instancetype)headerViewWithTableView:(UITableView *)tableView{
     5     static NSString *ID = @"header";
     6     HeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
     7     if (header == nil) {
     8         header = [[HeaderView alloc]initWithReuseIdentifier:ID];
     9     }
    10     return header;
    11 }
    12 
    13 - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
    14 {
    15     if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
    16         UIButton *nameView = [UIButton buttonWithType:UIButtonTypeCustom];
    17         // 背景图片
    18         [nameView setBackgroundImage:[UIImage imageNamed:@"发布新闻背景副本"] forState:UIControlStateNormal];
    19         [nameView setBackgroundImage:[UIImage imageNamed:@"welcome3"] forState:UIControlStateHighlighted];
    20         // 设置按钮内部的左边箭头图片
    21         [nameView setImage:[UIImage imageNamed:@"buddy_header_arrow副本"] forState:UIControlStateNormal];
    22         [nameView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    23         // 设置按钮的内容左对齐
    24         nameView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    25         // 设置按钮的内边距
    26         //        nameView.imageEdgeInsets
    27         nameView.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    28         nameView.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    29         [self.contentView addSubview:nameView];
    30         self.nameView = nameView;
    31         
    32         
    33         
    34         // 2.添加好友数
    35         UILabel *countView = [[UILabel alloc] init];
    36         countView.textAlignment = NSTextAlignmentRight;
    37         countView.textColor = [UIColor grayColor];
    38         [self.contentView addSubview:countView];
    39         self.countView = countView;
    40         
    41 
    42     }
    43         return self;
    44     }
    45 
    46 /**
    47  *  当一个控件的frame发生改变的时候就会调用
    48  *
    49  *  一般在这里布局内部的子控件(设置子控件的frame)
    50  */
    51 - (void)layoutSubviews
    52 {
    53     //一定要调用super的方法
    54     [super layoutSubviews];
    55     
    56     // 1.设置按钮的frame
    57     self.nameView.frame = self.bounds;
    58     
    59     // 2.设置数的frame
    60     CGFloat countY = 0;
    61     CGFloat countH = self.frame.size.height;
    62     CGFloat countW = 150;
    63     CGFloat countX = self.frame.size.width - 10 - countW;
    64     self.countView.frame = CGRectMake(countX, countY, countW, countH);
    65 }
    66 
    67 //设置数据
    68 - (void)setGroup:(MJFriendGroup *)group
    69 {
    70     _group = group;
    71     
    72     // 1.设置按钮文字(组名)
    73     [self.name setTitle:group.name forState:UIControlStateNormal];
    74     
    75     // 2.设置数量(总数)
    76     self.count.text = [NSString stringWithFormat:@"%lu", friends.count];
    77 }
    78 
    79 
    80 @end
  • 相关阅读:
    JAVA 8的新特性
    JAVA中map的分类和各自的特性
    设计模式之工厂方法模式(附实例代码下载)
    为什么重写equals还要重写hashcode??
    C# static的用法详解
    1-RadioButton控件的用法
    C#三种常用的读取XML文件的方法
    VS2017 中安装SVN
    pip安装selenium时,报错“You are using pip version 10.0.1, however version 18.0 is available.”的问题
    问题:unknown error: call function result missing 'value' 解决方法
  • 原文地址:https://www.cnblogs.com/chenhongios/p/4827529.html
Copyright © 2020-2023  润新知