标签:des style blog os 使用 io strong for ar
一、本次小例子截图:
二、代码如下:
#import <Foundation/Foundation.h> @interface Product : NSObject //标题 @property (strong, nonatomic) NSString * title; //描述 @property (strong, nonatomic) NSString * desc; //图片 @property (strong, nonatomic) NSString * imageName; @end
#import <UIKit/UIKit.h> @interface ViewController : UITableViewController<UIAlertViewDelegate> @end
#import "ViewController.h"
#import "Product.h"
@interface ViewController ()
@property (strong, nonatomic) NSMutableArray * arrProducts;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//0. 数组初始化
//实例化商品列表数组,如果能够准确知道数组容量的时候,不要用array直接实例化可变数组
self.arrProducts = [NSMutableArray arrayWithCapacity:50];
//1. 创建Model
for (int i=0; i<50; i++)
{
Product * pro = [[Product alloc] init];
pro.title = [NSString stringWithFormat:@"商品%i", i+1];
pro.desc = @"描述商品信息";
pro.imageName = [NSString stringWithFormat:@"00%i", arc4random_uniform(9)+1];
//2. 添加到数组
[self.arrProducts addObject:pro];
}
}
#pragma mark - tableView dataSource
#pragma mark 分组数量
#pragma mark 每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
{
return self.arrProducts.count;
}
#pragma mark 每行内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
Product * pro = self.arrProducts[indexPath.row];
//1. cell标题
cell.textLabel.text = pro.title;
//2. cell图标
cell.imageView.image = [UIImage imageNamed:pro.imageName];
//3. cell详细信息
cell.detailTextLabel.text = pro.desc;
//4. cell右侧图标
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //箭头
return cell;
}
#pragma mark TableView Delegate
//cell 高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
#pragma mark 点击某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Product * pro = self.arrProducts[indexPath.row];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"标题" message:@"我在学习TableView" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput]; //设置样式
[[alert textFieldAtIndex:0] setText:pro.title]; //赋值
[alert show];
}
#pragma mark - UIAlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//NSLog(@"%i", buttonIndex);
if (!buttonIndex) //取消
return;
//0. 获得修改
NSString * stringTitle = [alertView textFieldAtIndex:0].text;
//1. 修改model
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
Product * pro = self.arrProducts[indexPath.row];
pro.title = stringTitle;
//2. 刷新TableView
//2.1 刷新全部数据,当数量庞大时,不合适。(无动画效果)
//[self.tableView reloadData];
//刷新局部(有动画效果)
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationFade];
}
@end
三、 知识点记录
1. 引入 MVC的模型(Model),即:Product 。
1.1 声明了几个属性,为了处理对模型数据的修改。
1.2 初始化模型,并添加到数组中。
2. 设置Cell高度方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
3. 使用UIAlertView 及 代理。
4. 刷新tableView。
4.1 局部刷新 (有动画效果)
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationFade];
4.2 全部刷新(没有动画效果)
[self.tableView reloadData];
5. 补充一个MVC架构的图片。易于理解。
四、 问题记录
1. 像QQ, 微信 等, 发出的信息, 自动适配Cell高度, 怎么做到的?
2. UIAlertView 除了枚举的那几个样式, 怎么自定义UI ?
五、随笔
1. IOS app 有好多都是 UITableView 列表的效果。
2. 尽可能是理解基本属性,方法。 等熟了, 尝试 抽tableView。 加油!
标签:des style blog os 使用 io strong for ar
原文地址:http://www.cnblogs.com/iCodePhone/p/3937631.html