#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@end
@implementation ViewController
@synthesize tableView = _tableView;
@synthesize list = _list;
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *array = [[NSMutableArrayalloc] initWithObjects:@"美国",@"菲律宾",
@"黄岩岛",@"中国",@"泰国",@"越南",@"老挝",
@"日本" ,nil];
self.list = array;
_tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height)style:UITableViewStylePlain];
_tableView.delegate=self;
_tableView.dataSource=self;
[self.viewaddSubview:_tableView];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listcount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *TableSampleIdentifier =@"CellIdentifier";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:
TableSampleIdentifier];
if (cell ==nil) {
cell = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:TableSampleIdentifier];
}
NSUInteger row = [indexPathrow];
cell.textLabel.text = [self.listobjectAtIndex:row];
UIImage *image = [UIImageimageNamed:@"qq"];
cell.imageView.image = image;
UIImage *highLighedImage = [UIImageimageNamed:@"youdao"];
cell.imageView.highlightedImage = highLighedImage;
cell.detailTextLabel.text =@"打打打打";
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
/// 1.选择 利用cell的accessoryType 判断 是否选择
UITableViewCell *cell = [tableViewcellForRowAtIndexPath:indexPath];
if (cell.accessoryType ==UITableViewCellAccessoryNone) {
cell.accessoryType =UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType =UITableViewCellAccessoryNone;
}
// [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPathrow];
// 2. 删除
if (editingStyle ==UITableViewCellEditingStyleDelete) {
[self.listremoveObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
/// 3.移动
[self.tableViewsetEditing:YES];
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)
sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSUInteger fromRow = [sourceIndexPathrow];
NSUInteger toRow = [destinationIndexPathrow];
id object = [self.listobjectAtIndex:fromRow];
[self.listremoveObjectAtIndex:fromRow];
[self.listinsertObject:object atIndex:toRow];
}
// 4.添加
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.listinsertObject:@"你好"atIndex:0]; // 自定义添加数据(会自动刷新表)
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; /// 可以自定义插入的地方
[tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
}