ios中自定义table view cell

跟着书上的范例做完了一般的table view,然后做做自定义表格,也遇到一些问题,最后终于解决了,记录下怎么弄出来的吧。

知识点:

1.自定义表格cell的格式。

2.自定义表格cell的view类。

3.使用自定义的view来呈现数据。


一、自定义表格cell的格式

新建xib文件,拖table view cell,label,image view到画板上,如图:



二、自定义表格cell的view类
新建objective-c class,继承UITableViewCell类,见代码:
//
//  SimpleTableViewCellController.h
//  SimpleTableStoryBoradTest
//
//  Created by wong linwei on 12-9-8.
//  Copyright (c) 2012年 P&T. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SimpleTableViewCell : UITableViewCell {
    UIImageView *cellImage;
    UILabel *cellName;
    UILabel *cellTime;
}

@property (strong, nonatomic) IBOutlet UIImageView *cellImage;
@property (strong, nonatomic) IBOutlet UILabel *cellName;
@property (strong, nonatomic) IBOutlet UILabel *cellTime;

@end

//
//  SimpleTableViewCellController.m
//  SimpleTableStoryBoradTest
//
//  Created by wong linwei on 12-9-8.
//  Copyright (c) 2012年 P&T. All rights reserved.
//

#import "SimpleTableViewCell.h"

@implementation SimpleTableViewCell

@synthesize cellImage;
@synthesize cellName;
@synthesize cellTime;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
@end

三、使用自定义view类
在有table view的view controller类中实现UITableViewDelegate,UITableViewDataSource两个协议。
//
//  PNTViewController.h
//  SimpleTableStoryBoradTest
//
//  Created by wong linwei on 12-9-5.
//  Copyright (c) 2012年 P&T. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface PNTViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
    NSArray *tableData;
}
@end

//
//  PNTViewController.m
//  SimpleTableStoryBoradTest
//
//  Created by wong linwei on 12-9-5.
//  Copyright (c) 2012年 P&T. All rights reserved.
//

#import "PNTViewController.h"
#import "SimpleTableViewCell.h"

@implementation PNTViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    tableData = [NSArray arrayWithObjects:@"first",@"second",@"third",@"forth",@"fifth",@"sixth", nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"SimpleTableViewCell";
    SimpleTableViewCell *cell = (SimpleTableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier];//复用cell
    
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableViewCell" owner:self options:nil];//加载自定义cell的xib文件
        cell = [array objectAtIndex:0];
    }
    
    cell.cellName.text = [tableData objectAtIndex:indexPath.row];
    cell.cellTime.text = [NSString stringWithFormat:@"%@",[NSDate date]];
    cell.cellImage.image = [UIImage imageNamed:@"titlepic.png"];
    
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];//返回有多少项数据要显示
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;//此处返回cell的高度
}
@end

看下最后的结果吧:


出现的问题是:
1.运行是出现空白的表格,这种情况的发生,一是datadelegate和datasource两个IBOutlet没有连接上,二是因为给cell取的identifier不要跟复用的那个identifier一样。
2.在加载自定义cell布局的时候,发生crash现象,因为cell取得identifier跟复用的identifier一样了。
记得如果出不来,就去模拟器里面停止应用并且删除以后再run即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值