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

来源:互联网 发布:java和大数据的区别 编辑:程序博客网 时间:2024/05/11 13:02

写在前面

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

前面我们讲到了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