IOS学习之——表视图3 自定义单元格

写在前面

今天看新闻,科比肩部撕裂,可能会提前退役,这个顽固的男人,终于要落幕了,虽然我不是他的球迷,也是心生敬仰,今天的主题就以科比为素材,向这位人生的斗士致敬。

前面我们讲到了tableview的单元格的系统自带的image label,用起来很方便,可是毕竟限制很多,这一篇将会讲到一个神奇的东西——自定义单元格,由你控制单元格显示的内容,位置,形式。如下图,我们要制作一个球星列表,需要四项信息:头像+姓名+年龄+性别


设置界面

拖控件,如下图

设置单元格高度,这里要讲一下高度有两个:


  1. 设置tableview  的row  高度为:180  这个高度是tableview的行高,就是不管你要多大的cell,屏幕上都只能占一行的高度……
  2. 设置cell高度,——这个是cell本身的高度

在界面设置复用:


添加自定义控件类

添加自定义控件类:继承tableviewcell

<span style="font-size:14px;">#import <UIKit/UIKit.h>

@interface CustomTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *age;
@property (weak, nonatomic) IBOutlet UILabel *phone;

@end</span>

选择cell,让关联自定义类和storyboard


在tableview里面对自定义的类进行操作即可

#import "CustomTableViewController.h"
#import "CustomTableViewCell.h"

@interface CustomTableViewController ()<UITableViewDataSource>

@end

@implementation CustomTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 1;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier=@"Cell";
    CustomTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    cell.name.text=@"科比";
    cell.age.text=@"37";
    cell.phone.text=@"男";
    cell.image.image=[UIImage imageNamed:@"Kobe.png"];
    return cell;
}


相关代码

https://git.oschina.net/zhengaoxing/IOS_TableView

欢迎转载,转载请注明出处

本文地址:http://blog.csdn.net/zhenggaoxing/article/details/43086621

43086621



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值