iOS- 快速实现展示布局


看到这个界面,是不是觉得不像那种比较有规律的, 可以用 单独 tableViewCell 或者 xib 来实现方便些的,现在我直接在 C里快速实现展示布局.

先看布局, 可以分成两个分区:在数据源方法里去处理展现

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        static NSString *CellIdentifier = @"cell0";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor whiteColor];
        } else {
            for(UIView *view in cell.contentView.subviews) {
                [view removeFromSuperview];
            }
        }
// 处理第一分区
 return cell;
        
    } else if (indexPath.section == 1) {
        
        static NSString *CellIdentifier = @"cell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor whiteColor];
        } else {
            for(UIView *view in cell.contentView.subviews) {
                [view removeFromSuperview];
            }
        }
// 处理第二分区
  return cell;
    }
    return nil;
}

第一部分可以用两个标签去处理展示:

NSArray *nameArr = @[@"商品销售额",@"商品销售毛利",@"毛利率"];
        NSArray *valueArr = @[@"¥311.00",@"¥143.00",@"46.11%"];
        NSInteger count = nameArr.count;
        
        for (int i=0; i<count; i++) { // 循环创建两个标签
            UIView *backView = [[UIView alloc] init];
            backView.frame = CGRectMake(i*UI_View_Width/count, 30, UI_View_Width/count, 40);
            [cell.contentView addSubview:backView];
            
            UILabel *nameLabel = [[UILabel alloc] init];
            nameLabel.frame = CGRectMake(0, 0, UI_View_Width/count, 20);
            nameLabel.font = [UIFont systemFontOfSize:14];
            nameLabel.textColor = YYPColor(52, 53, 54);
            nameLabel.textAlignment = NSTextAlignmentCenter;
            nameLabel.text = nameArr[i];
            [backView addSubview:nameLabel];
            
            UILabel *valueLabel = [[UILabel alloc] init];
            valueLabel.frame = CGRectMake(0, 20, UI_View_Width/count, 20);
            valueLabel.font = [UIFont systemFontOfSize:16];
            valueLabel.textColor = YYPColor(255, 45, 77);
            valueLabel.textAlignment = NSTextAlignmentCenter;
            valueLabel.text = valueArr[i];
            [backView addSubview:valueLabel];
            
            if (i > 0) { // 添加间隔竖线
                UIView *line = [[UIView alloc] init];
                line.frame = CGRectMake(0, 0, 0.5, 40);
                line.backgroundColor = YYPColor(200, 200, 200);
                [backView addSubview:line];
            }
        }

第二部分可以利用一个标签创建通过富文本去修改布局:

NSArray *dataSource = @[
                                @{@"name":@"主粮系列", @"price1":@"85", @"price2":@"83", @"percent":@"4166"},
                                @{@"name":@"零食大全", @"price1":@"85", @"price2":@"83", @"percent":@"4166"},
                                ];
        
        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.frame = CGRectMake(12, 0, 90, 60);
        nameLabel.font = [UIFont systemFontOfSize:14];
        nameLabel.textColor = YYPColor(52, 53, 54);
        nameLabel.text = dataSource[indexPath.row][@"name"];
        [cell.contentView addSubview:nameLabel];
        
        for (int i=0; i<3; i++) { // 循环创建一个标签
            
            CGFloat magrinL = 12;
            
            UILabel *crossLabel = [[UILabel alloc]init];
            crossLabel.frame = CGRectMake(magrinL+90+i*(UI_View_Width-magrinL-90)/3.0, 0, (UI_View_Width-magrinL-90)/3.0, 60);
            crossLabel.font = [UIFont systemFontOfSize:14];
            crossLabel.textColor = YYPColor(52, 53, 54);
            crossLabel.numberOfLines = 0;
            crossLabel.textAlignment = NSTextAlignmentCenter;
            NSString *titleStr; // 标题
            NSString *valueStr; // 值
            if (i == 0) {
                titleStr = @"销售额";
                valueStr = [NSString stringWithFormat:@"¥%@", dataSource[indexPath.row][@"price1"]];
            } else if (i == 1) {
                titleStr = @"毛利";
                valueStr = [NSString stringWithFormat:@"¥%@", dataSource[indexPath.row][@"price2"]];
            } else if (i == 2) {
                titleStr = @"毛利率";
                valueStr = [NSString stringWithFormat:@"%@%%", dataSource[indexPath.row][@"percent"]];
            }
          // 创建通过富文本去修改色系
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n%@", titleStr, valueStr]];
            [string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(titleStr.length+1, valueStr.length)];
            [string addAttribute:NSForegroundColorAttributeName value:YYPColor(170, 170, 170) range:NSMakeRange(titleStr.length+1, valueStr.length)];
            
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            paragraphStyle.alignment = NSTextAlignmentCenter;//居中
            paragraphStyle.lineSpacing = 3; // 调整行间距
            [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
            crossLabel.attributedText = string;
            [cell.contentView addSubview:crossLabel];
            
            if (i > 0) { // 循环添加间隔横线
                UIView *line = [[UIView alloc] init];
                line.frame = CGRectMake(15, 59, UI_View_Width - 15, 0.5);
                line.backgroundColor = YYPColor(200, 200, 200);
                [cell.contentView addSubview:line];
            }
        }

这两个方法, 第一个布局用的较多, 第二个可以在商品介绍里用上富文本去修改色系.

**PS:建议像这种死的分区可以这么来,但是多数据需要后台传数据的那种最好用 MVC来实现. **
个人感觉后期维护修改界面布局的情况下, 直接在 cell 里修改会比较清晰方便.

第二种方法这里测试一下:
当以 MVC 形式来写的话, 标签我在 Cell 里还是用一个来先创建, 只不过多写一个label 的富文本布局方法.

/**
 * label 的富文本布局
 * 
 * titleStr 标题
 * ValueStr 值
 */
- (NSMutableAttributedString *)setupAttriLabelWithTitleStr:(NSString *)titleStr ValueStr:(NSString *)valueStr {
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n%@", titleStr, valueStr]];
    [string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(titleStr.length+1, valueStr.length)];
    [string addAttribute:NSForegroundColorAttributeName value:YYPColor(170, 170, 170) range:NSMakeRange(titleStr.length+1, valueStr.length)];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentCenter; // 居中
    paragraphStyle.lineSpacing = 3; // 调整行间距
    [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
    
    return string;
}

然后在 Model 赋值里调用这个方法

// model赋值
- (void)setModel:(YYPSalesMarginModel *)model {
    
    _model = model;
    
    // 商品系列名称
    self.name.text = [NSString stringWithFormat:@"%@", model.name];
    
    // 销售额
    self.sale.attributedText = [self setupAttriLabelWithTitleStr:@"销售额" ValueStr:[NSString stringWithFormat:@"¥%.2f", model.sale]];
    
    // 毛利
    self.grossProfit.attributedText = [self setupAttriLabelWithTitleStr:@"毛利" ValueStr:[NSString stringWithFormat:@"¥%.2f", model.grossProfit]];
    
    // 毛利率
    self.percent.attributedText = [self setupAttriLabelWithTitleStr:@"毛利率" ValueStr:[NSString stringWithFormat:@"%.2f%%", model.percent]];
}
MVC 创建效果图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,612评论 4 59
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,099评论 18 139
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,629评论 1 92
  • 星星是个在很多人面前显得非常沉默的一个人,但是私下里却非常活泼可爱,而且经常闹出很多笑话。星星常常在闺蜜们面前称自...
    星星fighting阅读 227评论 0 1
  • 囚徒困境(Prisoner's Dilemma)反映了个人最佳选择并非团体最佳选择。或者说在一个群体中,个人做出理...
    靳晓阳s阅读 723评论 0 0