zoukankan      html  css  js  c++  java
  • 【原】自定义tableViewCell的两种方法

    1、通过xib文件创建自定义cell

    ViewController.h  

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource>
    
    @property (strong, nonatomic) NSArray *listTeams;
    
    @end
    

      

     

    ViewController.m

    #import "ViewController.h"
    #import "CustomCell.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *plistPath = [bundle pathForResource:@"team" ofType:@"plist"];
        
        //获取属性列表中的全部数据
        self.listTeams = [[NSArray alloc] initWithContentsOfFile:plistPath];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [self.listTeams count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *cellIdentifier = @"Cell";
        
      //因为我是自定义的cell,加载的时候要进行初始化 static BOOL nibsRegistered = NO; if (!nibsRegistered) { UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil]; [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier]; nibsRegistered = YES; } CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; NSUInteger row = [indexPath row]; NSDictionary *rowDict = [self.listTeams objectAtIndex:row]; cell.myLabel.text = [rowDict objectForKey:@"name"]; cell.myLabel2.text = [rowDict objectForKey:@"name2"]; cell.myLabel3.text = [rowDict objectForKey:@"name3"]; NSString *imagePath = [rowDict objectForKey:@"image"]; imagePath = [imagePath stringByAppendingString:@".png"]; cell.myImageView.image = [UIImage imageNamed:imagePath]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; } @end

    CustomCell.h

    #import <UIKit/UIKit.h>
    
    @interface CustomCell : UITableViewCell
    
    @property (weak, nonatomic) IBOutlet UIImageView *myImageView;
    @property (weak, nonatomic) IBOutlet UILabel *myLabel;
    @property (weak, nonatomic) IBOutlet UILabel *myLabel2;
    @property (weak, nonatomic) IBOutlet UILabel *myLabel3;
    
    @end
    

    CustomCell.xib

    Identifier:Cell

    Main.storyboard 

    TableView不需要Cell

    2、在Main.storyboard中的TableViewCell中自定义Cell

    TableViewCell中的Identifier:Cell

    不需要初始化自定义的Cell

      

      

  • 相关阅读:
    CSS Sprites技术
    js Event对象
    iphone上做webapp时总会识别一串数字为手机号码并变黑显示
    获得touch事件,jquery绑定事件监听器,ios设备上打开touch状态以响应focus,active等伪类
    访问局域网内数据库
    理解javascript的闭包,原型,和匿名函数及IIFE
    socket跟TCP/IP 的关系,单台服务器上的并发TCP连接数问题
    最详细的Log4j使用教程
    [Java IO]06_JSON操作
    mybatis一次执行多条SQL语句
  • 原文地址:https://www.cnblogs.com/saurik/p/4801052.html
Copyright © 2011-2022 走看看