实现基本的表视图创建和显示实例,本例子实现了UITableView的选中行的信息编辑。
AppDelegate.h
//
// AppDelegate.h
// SelectCellDemo
//
// Created by Apple on 16/5/24.
// Copyright © 2016年 Apple. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
// 作为UITableView显示数据的两个NSArray
@property (strong, nonatomic) NSMutableArray* Timors;
@property (strong, nonatomic) NSMutableArray* details;
@end
AppDelegate.m
//
// AppDelegate.m
// SelectCellDemo
//
// Created by Apple on 16/5/24.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 创建、并初始化NSMutableArray对象。
self.Timors = [NSMutableArray arrayWithObjects:@"提莫1",
@"提莫2", @"提莫3" , @"提莫4", nil];
// 创建、并初始化NSMutableArray对象。
self.details = [NSMutableArray arrayWithObjects:
@"我去前面探探路",
@"我是一个约德尔人",
@"蘑菇怪是谁" ,
@"一二三四", nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
ViewController* viewController = [[ViewController alloc] init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
@end
ViewController.h
</pre><pre name="code" class="objc">//
// ViewController.h
// SelectCellDemo
//
// Created by Apple on 16/5/24.
// Copyright © 2016年 Apple. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
//
// ViewController.m
// SelectCellDemo
//
// Created by Apple on 16/5/24.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
#import "AppDelegate.h"
#import "DetailViewController.h"
// 定义应用程序委托对象
AppDelegate* appDelegate;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = [UIApplication sharedApplication].delegate;
UITableView* tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStyleGrouped];
UILabel* headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0)];
[headerLabel setText:@"提莫"];
headerLabel.textColor = [UIColor blackColor];
headerLabel.backgroundColor = [UIColor cyanColor];
headerLabel.textAlignment = NSTextAlignmentCenter;
//设置UITableView的页眉控件
[tableView setTableHeaderView:headerLabel];
UILabel* footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[footerLabel setText:@"主E副Q"];
//设置UITableView页脚控件
[tableView setTableFooterView:footerLabel];
//设置行cell高(默认44px)
[tableView setRowHeight:50];
//设置分割线颜色
[tableView setSeparatorColor:[UIColor redColor]];
//设置分割线风格
/**
* UITableViewCellSeparatorStyleNone 不使用分割线
UITableViewCellSeparatorStyleSingleLine 使用分割线
UITableViewCellSeparatorStyleSingleLineEtched 在有组的情况使用分割线
*/
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
// 设置UITableView的背景颜色
[tableView setBackgroundColor:[UIColor lightGrayColor]];
// 设置数据源代理,必须实现协议UITableViewDataSource中的相关方法
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return appDelegate.Timors.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 为表格行定义一个静态字符串作为标示符
static NSString* cellId = @"cellId";
// 从可重用表格行的队列中取出一个表格行
UITableViewCell* cell = [tableView
dequeueReusableCellWithIdentifier:cellId];
// 如果取出的表格行为nil
if(cell == nil)
{
// 创建一个UITableViewCell对象,使用默认风格
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellId];
}
// 从IndexPath参数中获取当前行的行号
NSUInteger rowNo = indexPath.row;
// 取出books中索引为rowNo的元素作为UITableViewCell的文本标题
cell.textLabel.text = [appDelegate.Timors objectAtIndex:rowNo];
// 将单元格的边框设置为圆角
cell.layer.cornerRadius = 12;
cell.layer.masksToBounds = YES;
// 为UITableViewCell的左端设置图片
cell.imageView.image = [UIImage imageNamed:@"1.jpg"];
// 为UITableViewCell的左端设置高亮状态视时的图片
cell.imageView.highlightedImage = [UIImage imageNamed:
@"11.jpg"];
// 取出details中索引为rowNo的元素作为UITableViewCell的详细内容
cell.detailTextLabel.text = [appDelegate.details objectAtIndex:rowNo];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
// 获取该应用的应用程序委托对象
appDelegate = [UIApplication
sharedApplication].delegate;
// // 获取Storyboard文件中ID为detail的视图控制器
DetailViewController* detailController = [[DetailViewController alloc] init];
// // 保存用户正在编辑的表格行对应的NSIndexPath
detailController.editingIndexPath = indexPath;
// // 让应用程序的窗口显示detailViewController
appDelegate.window.rootViewController = detailController;
}
@end
DetailViewController.h
//
// DetailViewController.h
// SelectCellDemo
//
// Created by Apple on 16/5/24.
// Copyright © 2016年 Apple. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
@property (strong, nonatomic) NSIndexPath* editingIndexPath;
@end
DetailViewController.m
//
// DetailViewController.m
// SelectCellDemo
//
// Created by Apple on 16/5/24.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "DetailViewController.h"
#import "ViewController.h"
#import "AppDelegate.h"
@interface DetailViewController ()
@end
// 定义正在编辑的表格行的行号
NSUInteger rowNo;
UITextField* nameField;
UITextField* detailField;
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
// 获取正在编辑的表格行的行号
rowNo = self.editingIndexPath.row;
// nameLabel
UILabel* nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 80, 30)];
[nameLabel setText:@"英雄:"];
[self.view addSubview:nameLabel];
// nameText
nameField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 250, 30)];
[nameField setBorderStyle:UITextBorderStyleRoundedRect];
nameField.backgroundColor = [UIColor whiteColor];
// 对nameField的text赋值
nameField.text = [appDelegate.Timors
objectAtIndex:rowNo];
[self.view addSubview:nameField];
// nameLabel
UILabel* detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 250, 80, 30)];
[detailLabel setText:@"详情:"];
[self.view addSubview:detailLabel];
detailField = [[UITextField alloc] initWithFrame:CGRectMake(100, 250, 250, 30)];
detailField.backgroundColor = [UIColor whiteColor];
[detailField setBorderStyle:UITextBorderStyleRoundedRect];
// 对detailField的text赋值
detailField.text = [appDelegate.details
objectAtIndex:rowNo];
[self.view addSubview:detailField];
UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(150, 300, 100, 100);
[button setTitle:@"完 成" forState:UIControlStateNormal];
[button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)onClick:(id)sender
{
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
// 替换appDelegate的books集合中指定索引处的元素
[appDelegate.Timors replaceObjectAtIndex:rowNo
withObject:nameField.text];
// 替换appDelegate的details集合中指定索引处的元素
[appDelegate.details replaceObjectAtIndex:rowNo
withObject:detailField.text];
// 获取Storyboard文件中ID为list的视图控制器
ViewController* listController = [[ViewController alloc] init];
// 控制程序窗口显示listController控制器
appDelegate.window.rootViewController = listController;
}
@end
项目目录结构:
效果图如下: