zoukankan      html  css  js  c++  java
  • iOS中自定义UITableViewCell的用法

    1、先创建一个View继承 UITableViewCell并使用xib快速建立模型。

    #import <UIKit/UIKit.h>
    #import "Score.h"
    
    @interface ShowScoreCell : UITableViewCell
    //在.h文件中声明对象
    @property(nonatomic,strong)Score *score;
    @end

    2、把需要的控件拖上xib并调整xib的大小

    3、把对应控件连线至.m文件中

    #import "ShowScoreCell.h"
    
    @interface ShowScoreCell()
    
    //以下属性都有连线 @property (weak, nonatomic) IBOutlet UILabel
    *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *positionLabel; @property (weak, nonatomic) IBOutlet UILabel *paperLabel; @property (weak, nonatomic) IBOutlet UILabel *scoreLabel; @property (weak, nonatomic) IBOutlet UIView *view; @end

    4、在调用实体的set方法时,给面面中需要的控件赋值

    - (void)setScore:(Score *)score{
        _score = score;
        self.nameLabel.text = score.name;
        self.positionLabel.text = score.position;
        self.paperLabel.text = score.paper;
        self.scoreLabel.text = score.score;
    }

    5、根据需要重写每个控件

    //重写
    -(void)layoutSubviews
    {
        [super layoutSubviews];
        
        if (self.selected)
        {
            //可以单独设置每一个Label选中时的背景
    //        self.nameLabel.backgroundColor = [UIColor redColor];
    //        self.positionLabel.backgroundColor = [UIColor redColor];
    //        self.paperLabel.backgroundColor = [UIColor redColor];
    //        self.scoreLabel.backgroundColor = [UIColor lightGrayColor];
            //也可以直接设置用来放Label的View的背景
            self.view.backgroundColor = [UIColor redColor];
            
        }
        
        
    }

    6、在视图控制器中做以下工作

     1> 拿到实体对象的数据,一般用懒加载的形式存在数组中。

    - (NSArray *)allScores{
        if (!_allScores) {
            _allScores = [[DBManager shareManager] allScore];
        }
        return _allScores;
    }

     2>在viewDidLoad方法中注册自定义的Cell

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self.navigationController setNavigationBarHidden:NO];
        self.navigationItem.title = @"查看成绩";
        //nibWithNibName:自定义Cell的名字
    [self.tableView registerNib:[UINib nibWithNibName:@"ShowScoreCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
    self.navigationItem.rightBarButtonItem
    = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(beginEditing:)];
    }

    提前声明一个cellIdentifier,在@implementation之前

    static NSString *cellIdentifier = @"scoreCell";

      3>在返回每个单元格时使用自定义的Cell样式

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        ShowScoreCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //    if (!cell) {
    //        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    //    }
        //设计cell
        Score *s = self.allScores[indexPath.row];
        cell.score = s;
        
        
        return cell;
    }
  • 相关阅读:
    LDA-math-MCMC 和 Gibbs Sampling
    本地blast详解
    【shell命令】拆分、合并、排序、比较文件
    数学中竟然还有这样的定理!
    数学之美番外篇:平凡而又神奇的贝叶斯方法
    glean工具和maker工具
    Perl语言中的ig
    批量替换某个目录下所有文件里的内容
    RepeatMasker使用
    Hi-C文库制备的连接酶连接效果
  • 原文地址:https://www.cnblogs.com/endtel/p/5368673.html
Copyright © 2011-2022 走看看