IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    OC之UITbaleView的用法

    键盘上的舞者发表于 2015-05-24 16:26:43
    love 0

    就和QQ和微信那样,每一个聊天的记录都是一个cell,上面展示的最最最简单的用法了。

    具体的实现代码:

    //
    //  ViewController.m
    //  淘宝物品展示
    //
    //  Created by 键盘上的舞者 on 15/5/24.
    //  Copyright (c) 2015年 键盘上的舞者. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "Shop.h"
    
    @interface ViewController ()
    {
        NSMutableArray *_shops;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        _shops=[NSMutableArray array];
    //    Shop *shop1=[Shop shopWithName:@"aaaa" ico:@"xx.png" desc:@"aaaaaaaa"];
    //    Shop *shop2=[Shop shopWithName:@"aaaa" ico:@"xx.png" desc:@"aaaaaaaa"];
    //    Shop *shop3=[Shop shopWithName:@"aaaa" ico:@"xx.png" desc:@"aaaaaaaa"];
    //    [_shops addObjectsFromArray:@[shop1,shop2,shop3]];
        for(int i=0;i<10;i++)
        {
            NSString *name=[NSString stringWithFormat:@"这是第%d个",i+1];
            NSString *ico=@"xx.png";
            NSString *desc=[NSString stringWithFormat:@"这是第%d个的描述",i+1];
            Shop *temp=[Shop shopWithName:name ico:ico desc:desc];
            [_shops addObject:temp];
        }
        
    }
    //只有一个组,这个方法不需要去实现,可以省略
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    //每一组中的数组行数
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return _shops.count;
    }
    //每个cell的详细内容
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
        //取出相应行对应的商品
        Shop *shop=_shops[indexPath.row];
        //设置标题
        cell.textLabel.text=shop.title;
        //设置图片
        NSString *imageName=[NSString stringWithFormat:@"%@",shop.ico];
        cell.imageView.image=[UIImage imageNamed:imageName];
        //设置描述
        cell.detailTextLabel.text=shop.desc;
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    //设置每个cell的高度
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 80;
    }
    //点击cell的时候做出的响应事件
    int indexRow;
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Shop *shop=_shops[indexPath.row];
        NSString *message=shop.desc;
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"这是一个弹窗" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        //设置弹窗的样式
        alert.alertViewStyle=UIAlertViewStylePlainTextInput;
        [alert textFieldAtIndex:0].text=message;
        [alert show];
        indexRow=indexPath.row;
    }
    //alertView代理方法
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if(buttonIndex==0)return;
        //获取编辑框的内容
        NSString *text=[alertView textFieldAtIndex:0].text;
        //将编辑框的内容更新到模型里面,并刷新数据
        Shop *shop=_shops[indexRow];
        shop.desc=text;
        //刷新
        [self.tableView reloadData];
    }
    
    
    
    
    
    
    
    
    
    
    
    
    @end



沪ICP备19023445号-2号
友情链接