通讯录图库界面,两种cell的定制

@interface FYZMalePersonCell : UITableViewCell

@property (nonatomic, retain) PhotoInfo *photoInfo;//接收c传入的M
<span style="background-color: rgb(153, 153, 255);">//动态设置cell的高度
+ (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo;</span>
@end<pre name="code" class="objc">@interface FYZMalePersonCell ()

@property (nonatomic, retain) UIImageView *iconView; //显示图标
@property (nonatomic, retain) UILabel *titleLabel; //显示名字与职称
@property (nonatomic, retain) UILabel *descriptionLabel; //显示图片描述
@property (nonatomic, retain) UIImageView *photoView; //显示图片
@end

@implementation FYZMalePersonCell


//重写setter
- (void)setPhotoInfo:(PhotoInfo *)photoInfo
{
    if (_photoInfo != photoInfo) {
        [_photoInfo release];
        _photoInfo = [photoInfo retain];
    }
    //为自身控件赋值
    //(1)图标
    self.iconView.image = [UIImage imageNamed:@"lanou"];
    //(2)名字加职称
    self.titleLabel.text = [NSString stringWithFormat:@"%@, %@", photoInfo.name, photoInfo.title];
    //(3)简介
    //修改descriptionLabel的高度
    CGRect desFrame = _descriptionLabel.frame;
    desFrame.size.height = [[self class] heightForText:photoInfo.introduction];//[self class]获取当前类
    _descriptionLabel.frame = desFrame;
    self.descriptionLabel.text = photoInfo.introduction;
    //(4)图片
    //修改photoView高度
    CGRect photoFrame = _photoView.frame;
    photoFrame.origin.y = desFrame.size.height + desFrame.origin.y;
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FYZ" ofType:@"png"]];
    photoFrame.size.height = [[self class] heightForImage:image];
    _photoView.frame = photoFrame;
    self.photoView.image = image;
    
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        //iconView
        CGFloat px = kPhotoCell_MarginLeft;
        CGFloat py = kPhotoCell_MarginBetween;
        CGFloat width = kPhotoCell_IconView_Width;
        CGFloat height = kPhotoCell_IconView_Height;
        self.iconView = [[UIImageView alloc] initWithFrame:CGRectMake(px, py, width, height)];
        [self.contentView addSubview:_iconView];
        [_iconView release];
        //titleLabel
        px = px + width + kPhotoCell_MarginLeft;
        py = py;
        width = kPhotoCell_TitleLabel_Width;
        height = kPhotoCell_TitleLabel_Height;
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(px, py, width, height)];
        _titleLabel.font = [UIFont systemFontOfSize:kFont_Size];
        _titleLabel.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_titleLabel];
        [_titleLabel release];
        //descriptionLabel
        px = kPhotoCell_MarginLeft;
        py = py + height + kPhotoCell_MarginBetween;
        width = kPhotoCell_Width;
        height = 0;
        self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(px, py, width, height)];
        _descriptionLabel.numberOfLines = 0;
        _descriptionLabel.font =  [UIFont systemFontOfSize:kFont_Size];
        [self.contentView addSubview:_descriptionLabel];
        [_descriptionLabel release];
        //photoView
        px = px;
        py = py + height + kPhotoCell_MarginBetween;
        width = kPhotoCell_Width;
        height = 0;
        self.photoView = [[UIImageView alloc] initWithFrame:CGRectMake(px, py, width, height)];
        [self.contentView addSubview:_photoView];
        [_photoView release];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

<span style="background-color: rgb(153, 153, 255);">+ (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo
{
    //1.图片高度</span>
     //让图片等比例缩放(防止图片失真)
    //(1)获取图片
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FYZ" ofType:@"png"]];
    CGFloat imageHeight = [self heightForImage:image];
    //2.文本的高度
    CGFloat textHeight = [self heightForText:photoInfo.introduction];
    //3.返回cell的总高度
    return kPhotoCell_TitleLabel_Height + imageHeight + textHeight + 3 * kPhotoCell_MarginBetween;
}
//单独计算图片的高度
<span style="background-color: rgb(153, 153, 255);">+ (CGFloat)heightForImage:(UIImage *)image
{</span>
    //(2)获取图片的大小
    CGSize size = image.size;
    //(3)求出缩放比例
    CGFloat scale = kPhotoCell_Width / size.width;
    CGFloat imageHeight = size.height * scale;
    return imageHeight;
}
<span style="background-color: rgb(153, 153, 255);">//单独计算文本的高度
+ (CGFloat)heightForText:(NSString *)text
{</span>
    //2.文本的高度
    //设置计算文本时,字体的大小,以什么标准来计算
    //第一个参数:限制范围 2个:设置文本高和间距 3个:字体的大小
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:kFont_Size]};
    CGSize textSize = [text boundingRectWithSize:CGSizeMake(kPhotoCell_Width, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return textSize.height;
}
- (void)dealloc
{
    RELEASE_SAFE(_descriptionLabel);
    RELEASE_SAFE(_iconView);
    RELEASE_SAFE(_titleLabel);
    RELEASE_SAFE(_photoView);
    RELEASE_SAFE(_photoInfo);
    [super dealloc];
}
@end
female cell
@interface FYZFemalePersonCell : UITableViewCell

@property (nonatomic, retain) PhotoInfo *photoInfo;

<span style="background-color: rgb(153, 153, 255);">//动态设置cell的高度
+ (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo;
@end</span>
@interface FYZFemalePersonCell ()
@property (nonatomic, retain) UIImageView *iconView; //显示图标
@property (nonatomic, retain) UILabel *titleLabel; //显示名字与职称
@property (nonatomic, retain) UILabel *descriptionLabel; //显示图片描述
@property (nonatomic, retain) UIImageView *photoView; //显示图片
@end
@implementation FYZFemalePersonCell

//重写setter方法为控件赋值
- (void)setPhotoInfo:(PhotoInfo *)photoInfo
{
    if (_photoInfo != photoInfo) {
        [_photoInfo release];
        _photoInfo = [photoInfo retain];
    }
    //赋值
    //(1)图标
    self.iconView.image = [UIImage imageNamed:@"lanou"];
    //(2)titleLabel
    self.titleLabel.text = [NSString stringWithFormat:@"%@, %@", photoInfo.name, photoInfo.title];
    //(3)图片
    //修改photoView高度
    CGRect photoFrame = _photoView.frame;
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FYZ" ofType:@"png"]];
    photoFrame.size.height = [[self class] heightForImage:image];
    //_photoView.frame = photoFrame;
    self.photoView.image = image;
    //(4)简介
    //修改descriptionLabel的高度
    CGRect desFrame = _descriptionLabel.frame;
    desFrame.size.height = [[self class] heightForText:photoInfo.introduction];//[self class]获取当前类
    desFrame.origin.y = photoFrame.size.height + photoFrame.origin.y;
    _descriptionLabel.frame = desFrame;
    self.descriptionLabel.text = photoInfo.introduction;
    
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        //titleLabel
        CGFloat px = kPhotoCell_MarginLeft;
        CGFloat py = kPhotoCell_MarginBetween;
        CGFloat width = kPhotoCell_TitleLabel_Width;
        CGFloat height = kPhotoCell_TitleLabel_Height;
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(px, py, width, height)];
        [self.contentView addSubview:_titleLabel];
        [_titleLabel release];
        //iconView
        px = px + width + kPhotoCell_MarginLeft;
        py = py;
        width = kPhotoCell_IconView_Width;
        height = kPhotoCell_IconView_Height;
        self.iconView = [[UIImageView alloc] initWithFrame:CGRectMake(px, py, width, height)];
        [self.contentView addSubview:_iconView];
        [_iconView release];

        //descriptionLabel
        px = kPhotoCell_MarginLeft;
        py = py + height + kPhotoCell_MarginBetween;
        width = kPhotoCell_Width;
        height = 0;
        self.photoView = [[UIImageView alloc] initWithFrame:CGRectMake(px, py, width, height)];
        [self.contentView addSubview:_photoView];
        [_photoView release];
        //photoView
        px = px;
        py = py + height;
        width = kPhotoCell_Width;
        height = 0;
        self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(px, py, width, height)];
        _descriptionLabel.font = [UIFont systemFontOfSize:kFont_Size];
        _descriptionLabel.numberOfLines = 0;
        [self.contentView addSubview:_descriptionLabel];
        [_descriptionLabel release];
    }
    return self;
}
+ (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo
{
    //1.图片高度
    //让图片等比例缩放(防止图片失真)
    //(1)获取图片
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FYZ" ofType:@"png"]];
    CGFloat imageHeight = [self heightForImage:image];
    //2.文本的高度
    CGFloat textHeight = [self heightForText:photoInfo.introduction];
    //3.返回cell的总高度
    return kPhotoCell_TitleLabel_Height + imageHeight + textHeight + 3 * kPhotoCell_MarginBetween;
}
//单独计算图片的高度
+ (CGFloat)heightForImage:(UIImage *)image
{
    //(2)获取图片的大小
    CGSize size = image.size;
    //(3)求出缩放比例
    CGFloat scale = kPhotoCell_Width / size.width;
    CGFloat imageHeight = size.height * scale;
    return imageHeight;
}
//单独计算文本的高度
+ (CGFloat)heightForText:(NSString *)text
{
    //2.文本的高度
    //设置计算文本时,字体的大小,以什么标准来计算
    //第一个参数:限制范围 2个:设置文本高和间距 3个:字体的大小
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:kFont_Size]};
    CGSize textSize = [text boundingRectWithSize:CGSizeMake(kPhotoCell_Width, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return textSize.height;
}


- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (void)dealloc
{
    RELEASE_SAFE(_descriptionLabel);
    RELEASE_SAFE(_iconView);
    RELEASE_SAFE(_titleLabel);
    RELEASE_SAFE(_photoView);
    [super dealloc];
}
@end

@interface <span style="background-color: rgb(153, 153, 255);">FYZPhotoPersonHelper</span> : NSObject

+ (FYZPhotoPersonHelper *)sharedPhotoPersonHelper;
//分区数
+ (NSInteger)numberOfSections;
//分区元素
+ (NSInteger) numberOfRowsInSection:(NSInteger)section;
//返回photoInfo对象
+ (PhotoInfo *)photoInfoAtIndexPath:(NSIndexPath *)indexPath;
@end

@interface FYZPhotoPersonHelper ()

@property (nonatomic, retain) NSMutableArray *photoArr;

@end

@implementation FYZPhotoPersonHelper

static FYZPhotoPersonHelper *helper = nil;

+ (FYZPhotoPersonHelper *)sharedPhotoPersonHelper
{
    @synchronized(self) {
        if (!helper) {
            helper = [[FYZPhotoPersonHelper alloc] init];
            [helper readDataFromPlist];
        }
    }
    return helper;
}
- (void<span style="background-color: rgb(153, 153, 255);">)readDataFromPlist</span>
{
    //获取路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"PresonList" ofType:@"plist"];
    //根据路径初始化数组对象
    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
    //创建可变数组,存储封装好的photoInfo对象
    self.photoArr = [NSMutableArray array];
    //遍历数组,将小子点封装成photoInfo对象
    for (NSDictionary *tempDic in arr) {
        PhotoInfo *photo = [[PhotoInfo alloc] initWithDic:tempDic];
        [_photoArr addObject:photo];
        RELEASE_SAFE(photo);
    }
}
+ (NSInteger)numberOfSections
{
    //先调用这个方法创建helper
    [FYZPhotoPersonHelper sharedPhotoPersonHelper];
    return 1;
}
+ (NSInteger) numberOfRowsInSection:(NSInteger)section
{
    return helper.photoArr.count;
}
+ (PhotoInfo *)photoInfoAtIndexPath:(NSIndexPath *)indexPath
{
    return helper.photoArr[indexPath.row];
}
@end


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android中,实现从一个界面跳转到联系人(通讯录)的多个子界面,通常涉及到`Activity`管理和数据通信。以下是一个简单的步骤说明: 1. **创建Activity栈**: 创建一个包含多个子Activity的主Activity,比如`ContactListActivity`,这是展示整个联系人列表的地方。每个子活动代表一个特定的联系人列表筛选或详细信息查看界面。 2. **启动Activity**: 当用户选择某个操作(如搜索、点击电话号码等),在对应的`ContactListActivity`内部调用`startActivityForResult()`,传递一个Intent和一个请求码,指定要打开哪个子Activity。例如: ```java Intent intent = new Intent(this, ContactDetailActivity.class); startActivityForResult(intent, REQUEST_DETAIL); ``` 3. **接收结果**: 子Activity执行完后,会通过`onActivityResult()`回调将结果返回给父Activity。你可以在这里处理用户的操作结果。 4. **定义子Activity**: 为每个子界面创建单独的Activity,如`ContactFilterActivity`, `ContactDetailActivity`。这些Activity可能还需要一个公共的基类,继承自`FragmentActivity`或`AppCompatActivity`,以便于管理事务和生命周期。 5. **数据传递**: 如果子Activity需要从父Activity获取参数或状态,可以通过Intent extras传递数据,或者使用保存在`SharedPreferences`中的键值对。 6. **界面间通信**: 使用Android的数据绑定机制,如`ViewModel`、`LiveData`或者事件总线(如EventBus)来实现实时的状态更新和事件传递。 7. **Activity跳转管理**: Android提供了`FragmentManager`或`Navigation Component`来管理 Activity/Fragment 的导航和回退栈,确保用户的操作流程顺畅。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值