UITableViewCell的简单应用-书架

本文介绍了一个iOS平台上的图书阅读应用实现方法,通过使用UITableView展示不同类型的书籍,并详细讲解了从初始化视图控制器到子视图的加载过程。此外,还介绍了如何通过自定义单元格显示书籍类型和名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@interface MainViewController ()


@property (nonatomic, retain)NSMutableArray *array;

@property (nonatomic, retain)UITableView *tableView;


@end


@implementation MainViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

//        self.array = [NSMutableArray arrayWithObjects: @"玄幻", @"言情", @"耽美",@"都市" ,@"网游", nil];

        

        NSMutableArray *arr1 =  [NSMutableArray arrayWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"斗破苍穹", @"title", @"k.png", @"image", @"天蚕土豆", @"author", nil],  [NSMutableDictionary dictionaryWithObjectsAndKeys:@"完美世界", @"title", @"k.png", @"image", @"辰东", @"author", nil], [NSMutableDictionary dictionaryWithObjectsAndKeys:@"仙逆", @"title", @"4.png", @"image", @"耳根", @"author", nil],nil];

        

        NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"当你孤单你会想起谁", @"title", @"k.png", @"image", @"饶雪漫", @"author", @"cicicici1", @"intro", nil], [NSMutableDictionary dictionaryWithObjectsAndKeys:@"放弃你,下辈子吧!", @"title", @"k.png", @"image", @"桩桩", @"author", @"cicicici2", @"intro", nil], [NSMutableDictionary dictionaryWithObjectsAndKeys:@"独家记忆", @"title", @"k.png", @"image", @"木浮生", @"author", @"cicicici3", @"intro", nil], [NSMutableDictionary dictionaryWithObjectsAndKeys:@"终有一爱", @"title", @"k.png", @"image", @"金陵雪", @"author", @"cicicici4", @"intro", nil],nil];


        

       self.array = [NSMutableArray arrayWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"玄幻",@"type", arr1, @"book", nil], [NSMutableDictionary dictionaryWithObjectsAndKeys:@"言情",@"type", arr2, @"book", nil], nil];


        

        self.tableView = [[UITableView alloc] init];

        [_tableView release];

        self.title = @"书架";

    }

    return self;

}


- (void)dealloc

{

    [_tableView release];

    

    [_array release];

    [super dealloc];


}

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:(UITableViewStyleGrouped)];

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    [self.view addSubview:self.tableView];

    [_tableView release];

    

    

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NameViewController *nameVC = [[NameViewController alloc] init];

    nameVC.self.title = [[self.array objectAtIndex:indexPath.row] objectForKey:@"type"];

    nameVC.self.array1 = [[self.array objectAtIndex:indexPath.row] objectForKey:@"book"];

    [self.navigationController pushViewController:nameVC animated:YES];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{


   return  [self.array count];


}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

        static NSString *str = @"kzss";

   MyCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

    if (nil == cell) {

        cell = [[[MyCellTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:str] autorelease];

    }

    NSString *name = [[self.array objectAtIndex:indexPath.row] objectForKey:@"type"];

    cell.myTitleLabel.text = name;

    return cell;

    

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}




@end


@interface NameViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>



@property (nonatomic, retain)NSMutableArray *array1;

@end


#import "NameViewController.h"

#import "BookViewController.h"

#import "MyCellTableViewCell.h"

@interface NameViewController ()



@end


@implementation NameViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

        self.array1 = [NSMutableArray array];

    }

    return self;

}


- (void)dealloc

{

    [_array1 release];

    [super dealloc];


}

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:(UITableViewStyleGrouped)];

    table.delegate = self;

    table.dataSource = self;

    [self.view addSubview:table];

    [table release];

    

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    BookViewController *bookVC = [[BookViewController alloc] init];

    bookVC.title = [[self.array1 objectAtIndex:indexPath.row] objectForKey:@"title"];

    bookVC.name3 = [[self.array1 objectAtIndex:indexPath.row] objectForKey:@"author"];

    bookVC.imagename = [[self.array1 objectAtIndex:indexPath.row] objectForKey:@"image"];

    [self.navigationController pushViewController:bookVC animated:YES];

    [bookVC release];


}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

   

    return  [self.array1 count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *str1 = @"aa";

    MyCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str1];

    if (nil == cell) {

        cell = [[[MyCellTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:str1] autorelease];

    }

    

    NSString *name = [[self.array1 objectAtIndex:indexPath.row] objectForKey:@"title"];

    cell.myTitleLabel.text = name;

    return cell;

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end


@interface BookViewController : UIViewController



@property (nonatomic,retain)UILabel *name3;

@property (nonatomic,retain)UILabel *from;

@property (nonatomic,retain)NSString *imagename;

@end


@interface BookViewController ()


@end


@implementation BookViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

      

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    

     NSString *name = self.imagename;

    

    UIImageView *imagek = [[UIImageView alloc] initWithFrame:(CGRectMake(10, 70, 200, 200))];

    imagek.backgroundColor =[UIColor cyanColor];


    imagek.image =[UIImage imageNamed:name];

    [self.view addSubview:imagek];

    [imagek release];

    

    UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(220, 70, 80, 60))];

    label.textAlignment = NSTextAlignmentCenter;

    label.text = [NSString stringWithFormat:@"%@", self.name3];

    [self.view addSubview:label];

    [label release];

    

    UILabel *label1 = [[UILabel alloc] initWithFrame:(CGRectMake(220, 150, 80, 60))];

    label1.backgroundColor = [UIColor greenColor];

    [self.view addSubview:label1];

    [label1 release];

    

    

    UIImageView *image1 = [[UIImageView alloc] initWithFrame:(CGRectMake(10, 275, 300, 200))];

    image1.backgroundColor =[UIColor blueColor];

    image1.image = [UIImage imageNamed:name];

    [self.view addSubview:image1];

    [image1 release];


}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



@interface MyCellTableViewCell : UITableViewCell


@property (nonatomic, retain)UILabel *myTitleLabel;


@end


@implementation MyCellTableViewCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        // Initialization code

      

        self.myTitleLabel = [[UILabel alloc] init];

        self.myTitleLabel.textAlignment = NSTextAlignmentCenter;

        [self.contentView addSubview:self.myTitleLabel];

        [_myTitleLabel release];

    }

    return self;

}


- (void)dealloc

{

    [_myTitleLabel release];

    [super dealloc];

}


- (void)layoutSubviews

{

    [super layoutSubviews];

    

    self.myTitleLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

    

}- (void)awakeFromNib

{

    // Initialization code

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated

{

    [super setSelected:selected animated:animated];


    // Configure the view for the selected state

}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值