iOS中一个tableView中显示两种cell的问题

来源:互联网 发布:宣传片制作软件下载 编辑:程序博客网 时间:2024/05/22 06:25

首先上效果图,关于昵称 时间的显示用到了富文本,后面会介绍.

1 首先说一下,在一个tableView中显示两个cell的问题 ,以XIB为例

上代码

 /**

     *  注册cell

     */

    [self.tableListregisterNib:[UINibnibWithNibName:@"CKFriendsCell"bundle:nil]forCellReuseIdentifier:@"CKFriendsCell"];

    

    [self.tableListregisterNib:[UINibnibWithNibName:@"CKFriendFocusCell"bundle:nil]forCellReuseIdentifier:@"CKFriendFocusCell"];

    

}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    

    returnself.arraySource.count;

    

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    

    CKFriendsModel *friendModel = self.arraySource[indexPath.row];

    

    if (friendModel.type ==101) {//用户关注了某人

        

        CKFriendFocusCell *mycell = [tableViewdequeueReusableCellWithIdentifier:@"CKFriendFocusCell"];

        

        mycell.friendModel = self.arraySource[indexPath.row];

        

        //iconView添加单点手势

        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(clickImageView)];

        

        mycell.iconView.userInteractionEnabled =YES;

        

        [mycell.iconView addGestureRecognizer:tapGesturRecognizer];

        

        //去除选中时候的灰色背景

        mycell.selectionStyle =UITableViewCellSelectionStyleNone;

        

        return mycell;

    }

    

    CKFriendsCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"CKFriendsCell"];

    

    cell.friendModel = self.arraySource[indexPath.row];

    

    //去除选中时候的灰色背景

    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    

    return cell;

}


一定要记得注册两种cell  然后在你需要显示不同cell的位置赋值就可以了.

2 关注富文本的问题

富文本在iOS开发中多用于图文混排 还是比较强大的,我们一起来看一下他的头文件吧

     

实例化方法:

使用字符串初始化

- (id)initWithString:(NSString *)str;

例:

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"我是bboyhzigang"];


- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;


字典中存放一些属性名和属性值,如:

NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:

                                    [UIFontsystemFontOfSize:15.0],NSFontAttributeName,

                                    [UIColorredColor],NSForegroundColorAttributeName,

                                   NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"我是bboyhzigang" attributes:attributeDict];

- (id)initWithAttributedString:(NSAttributedString *)attester;

使用NSAttributedString初始化,跟NSMutableString,NSString类似


使用方法:

为某一范围内文字设置多个属性

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;

为某一范围内文字添加某个属性

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

为某一范围内文字添加多个属性

- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;

移除某范围内的某个属性

- (void)removeAttribute:(NSString *)name range:(NSRange)range;


 常见的属性及说明

NSFontAttributeName 字体

NSParagraphStyleAttributeName 段落格式 

NSForegroundColorAttributeName 字体颜色

NSBackgroundColorAttributeName  背景颜色

NSStrikethroughStyleAttributeName删除线格式

NSUnderlineStyleAttributeName     下划线格式

NSStrokeColorAttributeName       删除线颜色

NSStrokeWidthAttributeName删除线宽度

NSShadowAttributeName 阴影

更多方法和属性说明详见苹果官方说明文档:

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003689

上代码


 //朋友名字

        NSString *nameStr = friendModel.actor[@"name"];

        

        str = [NSString stringWithFormat:@"%@  %@",friendModel.actor[@"name"],dateString];

        

        NSMutableAttributedString *attstr = [[NSMutableAttributedStringalloc] initWithString:str];

        

        [attstr addAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:12.f]

                                } range:NSMakeRange(0, nameStr.length)];

        

        [attstr addAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:9.f]

                                } range:NSMakeRange(nameStr.length +2,dateString.length)];

        

        self.nameLabel.attributedText = attstr;


这里只是简单的对几个字符串进行了字体的改变 ,具体的排版要根据需求来,更多的设置大家可以点进头文件根据需要找出对应的属性和方法.
0 0
原创粉丝点击