码迷,mamicode.com
首页 > 其他好文 > 详细

开发进阶14_UITableView单行显示

时间:2014-10-28 00:20:50      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:des   style   io   color   os   ar   使用   for   sp   

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    

}

 

#pragma mark - 一共一组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

 

#pragma mark - 这一组里面有多少行

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

{

    return 9;

}

 

#pragma mark - 返回第indexPath这行对应的内容

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

{

    /*

     Default:不显示detailTextLabel

     Value1:在右边显示detailTextLabel

     Value2:不显示图片,显示detailTextLabel

     Subtitle:在底部显示detailTextLabel

     */

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    

    cell.textLabel.text = [NSString stringWithFormat:@"产品-%ld",indexPath.row];

    

 

    //设置详情文字

    cell.detailTextLabel.text = [NSString stringWithFormat:@"产品-%ld好啊!!!!",indexPath.row];

    

    //设置图片

    NSString *imageName = [NSString stringWithFormat:@"00%ld.png",indexPath.row + 1];

    cell.imageView.image = [UIImage imageNamed:imageName];

    

    //设置右边的小图标

    //大于号图标

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    //没有图标

    cell.accessoryType = UITableViewCellAccessoryNone;

    //钩号图标

    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    //圆圈中有个i的图标

    cell.accessoryType = UITableViewCellAccessoryDetailButton;

    //大于号图标和圆圈中有个i的图标组合

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

 

    

    //设置最右边显示什么控件

    cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];

    

        return cell;

    

}

 

#pragma mark - 代理方法

#pragma mark 返回indexPath这行cell的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 70;

}

 

@end

 
 
采用模型的方式重构,同时弹出对话框修改name属性
 

@interface Product : UIView

//名称

@property (nonatomic, copy) NSString *name;

//描述

@property (nonatomic, copy) NSString *desc;

//图标

@property (nonatomic, copy) NSString *icon;

+ (id)shopWithName:(NSString *)name icon:(NSString *)icon desc:(NSString *)desc;

@end

 

+ (id)shopWithName:(NSString *)name icon:(NSString *)icon desc:(NSString *)desc

{

    Product *shop = [[Product alloc] init];

    shop.name = name;

    shop.desc = desc;

    shop.icon = icon;

    return shop;

 

}

 

#import "ViewController.h"

#import "Product.h"

 

@interface ViewController () <UITableViewDataSource, UITableViewDelegate,UIAlertViewDelegate>

{

    NSMutableArray *_allShop;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //创建数组

    _allShop = [NSMutableArray array];

    

    //创建数据

    Product *shop1 = [Product shopWithName:@"图书/音像" icon:@"001.png" desc:@"小说、情感、卡拉OK"];

    Product *shop2 = [Product shopWithName:@"母婴用品" icon:@"002.png" desc:@"奶粉!!!!"];

    Product *shop3 = [Product shopWithName:@"玩具" icon:@"003.png" desc:@"汽车、飞机、轮船"];

    

    [_allShop addObjectsFromArray:@[shop1,shop2,shop3]];

    //添加其他假数据

    for (int i = 0; i < 20; i++) {

        NSString *name = [NSString stringWithFormat:@"随机产品- %d",i];

        NSString *desc = [NSString stringWithFormat:@"%@--好好",name];

        NSString *icon = [NSString stringWithFormat:@"00%d.png",(i % 9) + 1];

        Product *shop = [Product shopWithName:name icon:icon desc:desc];

        

        [_allShop addObject:shop];

    }

    

    

}

 

#pragma mark - 一共一组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

 

#pragma mark - 这一组里面有多少行

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

{

    return _allShop.count;

}

 

#pragma mark - 返回第indexPath这行对应的内容

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

{

 

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    //获取这一行的产品

    Product *shop = _allShop[indexPath.row];

    

    cell.textLabel.text = shop.name;

 

    //设置详情文字

    cell.detailTextLabel.text = shop.desc;

    

    //设置图片

 

    cell.imageView.image = [UIImage imageNamed:shop.icon];

    //设置最右边显示什么控件

    cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];

    

        return cell;

    

}

 

#pragma mark - 代理方法

#pragma mark 选中某一行的cell就会调用

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

{

    //取出所点击这行的产品对象

    Product *shop = _allShop[indexPath.row];

    

    //1.创建弹框

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"产品展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

    

    //设置样式(一个明文文本框)

    alert.alertViewStyle = UIAlertViewStylePlainTextInput;

    //设置文本框默认位置

    [alert textFieldAtIndex:0].text = shop.name;

    

    //2.显示弹框

    [alert show];

    

    //3.绑定行号为alertview的tag

    alert.tag = indexPath.row;

    

}

两个方法之间的关联是:先调用tableView:didSelectRowAtIndexPath:,在这个方法中,给UIAlertView的tag赋值为修改的行号,然后再alertView:clickedButtonAtIndex:方法中就可以直接使用了

 

#pragma mark - alertView的代理方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if(buttonIndex == 0) return;

    

    //1.取出文本框文字

    NSString *text = [alertView textFieldAtIndex:0].text;

    

    //2.将文字更新到对应的cell上面去

    Product *shop = _allShop[alertView.tag];

    shop.name = text;

    

    //3.刷新表格

    /*

     重新向数据源索取数据

     重新向数据源发送消息

     重新调用数据源的方法,根据返回值决定显示什么数据

     */

    //整体刷新

    [_tableView reloadData];

    

    //局部刷新

    //刷新第inSection组的第indexPathForRow行

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0];

    NSArray *paths = @[indexPath];

    //刷新哪一行,使用什么样的动画

    [_tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];

}

 

@end

开发进阶14_UITableView单行显示

标签:des   style   io   color   os   ar   使用   for   sp   

原文地址:http://www.cnblogs.com/yaofch107/p/4055449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!