.h文件
//
// ViewController.h
// UITableView
//
// Created by on 13-9-24.
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray * datalist;
-(IBAction)addAddress:(id)sender ;
-(IBAction)redAddress:(id)sender ;
@end
.m文件
//
// ViewController.m
// UITableView
//
// Created by on 13-9-24.
// Copyright (c) 2013年 . All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize datalist;
@synthesize myTableView;
//初始化加载数据
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *list =[[NSArray alloc]initWithObjects:@"北京",@"成都",@"上海",@"广州",@"深圳",@"西安",@"重庆",@"武汉",@"大连",nil];
self.datalist = list;
self.myTableView.separatorColor = [UIColor blueColor];//分割线颜色
//启动表格的编辑模式
[self.myTableView setEditing:YES animated:YES];
}
-(IBAction)addAddress:(id)sender
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"已经点击增加地址" message:@"增加地址成功!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
[alertView show];
}
-(IBAction)redAddress:(id)sender
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//填充每个Cell
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellWithIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];
}
NSUInteger row = [indexPath row];
//cell.imageView.image = [UIImage imageNamed:@"A.jpg"];
cell.textLabel.text = [self.datalist objectAtIndex:row];
[[cell imageView] setImage:[UIImage imageNamed:@"Y.png"]];
cell.detailTextLabel.text = @"详细信息";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//显示左边的箭头指示
cell.selectionStyle = UITableViewCellSelectionStyleGray;//选中颜色
return cell;
}
#pragma mark- 移动
//这个方法用来告诉表格 这一行是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//这个方法就是执行移动操作的
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)
sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSUInteger fromRow = [sourceIndexPath row];
NSUInteger toRow = [destinationIndexPath row];
id object = [datalist objectAtIndex:fromRow];
[datalist removeObjectAtIndex:fromRow];
[datalist insertObject:object atIndex:toRow];
}
// 改变行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
//删除、增加按钮风格
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
//相应添加删除按钮(与返回风格有关)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSUInteger row = [indexPath row];
[self.datalist removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
else
{
//我们实现的是在所选行的位置插入一行,因此直接使用了参数indexPath
NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
//同样,将数据加到list中,用的row
NSUInteger row = [indexPath row];
[self.datalist insertObject:@"新添加的行" atIndex:row ];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
}
}
//顶部标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @" UITableView";
}
//底部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @" HelloWorld";
}
//返回section行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [datalist count];
}
//删除时按钮上显示的文字
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
//预处理机制
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] % 2 == 0) {
cell.backgroundColor = [UIColor whiteColor];
} else {
cell.backgroundColor = [UIColor greenColor];
}
}
@end