你好,游客 登录 注册 搜索
背景:
阅读新闻

iOS中自定义Table View Cell

[日期:2013-04-12] 来源:Linux社区  作者:ab_ba [字体:大 中 小]

三、使用自定义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即可。
linux
相关资讯       iOS自定义View  iOS自定义Cell 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款