首页 > 其他 > 详细

应用程序之Xib自定义Cell

时间:2014-04-10 05:04:02      阅读:494      评论:0      收藏:0      [点我收藏+]
  • 效果展示
  • 结构分析
  • 代码实现

一、效果展示

bubuko.com,布布扣

二、结构分析

1??首先我们让我们的控制器不再继承UIViewController,而是继承UITableViewController。这样就直接遵守了delegate协议、dataSource协议

2??数据使用模型来加载

3??自定义的Cell使用单独的类来管理

三、代码实现

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣
//
//  BookController.m
//  01-自定义cell04
//
//  Created by apple on 14-4-9.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import "BookController.h"
#import "Book.h"
#import "BookCell.h"

@interface BookController ()
{
    NSMutableArray *books;
}
@end

@implementation BookController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    books = [NSMutableArray array];
    for (int i = 0; i<20; i++) {
        Book *b = [Book bookWithName:[NSString stringWithFormat:@"iOS开发%d", i] price:arc4random_uniform(200)/10.0 + 10];
        [books addObject:b];
    }

}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return books.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    //加载的cell就是xib对应的BookCell对象
    BookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:nil options:nil] lastObject];
        
    }
    
    Book *book = books[indexPath.row];
    cell.nameLabel.text = book.name;
    cell.priceLable.text = [NSString stringWithFormat:@"¥%.1f", book.price];
    
    UIButton *collect = cell.collectBtn;
    [collect addTarget:self action:@selector(collectClick: event:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (void)collectClick:(UIButton*)btn event:(UIEvent*)event
{
    
    //NSLog(@"----%@", event);
    UITableView *tableView = (UITableView *)self.view;
    
    // 获取所有的触摸点(UITouch对象,如果是单点触碰,就只有1个UITouch)
    NSSet *touches = [event allTouches];
    
    // 一个UITouch对象对应一根手指
    UITouch *touch = [touches anyObject];
    
    // 获取触摸点在UITableView上面的的位置
    CGPoint position = [touch locationInView:tableView];
    
    // 根据触摸位置 得到 对应的行号
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:position];
    //NSLog(@"%d", indexPath.row);
    
    Book *book = books[indexPath.row];
    NSLog(@"%@", book.name);
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消选中蓝色
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
bubuko.com,布布扣

 

应用程序之Xib自定义Cell,布布扣,bubuko.com

应用程序之Xib自定义Cell

原文:http://www.cnblogs.com/letougaozao/p/3654404.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!