UITableView的实现过程,滑动,alter弹窗删除对象

来源:互联网 发布:淘宝二手卡地亚手表 编辑:程序博客网 时间:2024/05/11 13:33

UITableView的实现过程,其中包括滑动删除,alter弹窗删除。

////  ViewController.m//  tableTest////  Created by wu on 14-8-20.//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    // 初始化tableView的数据    NSMutableArray *list = [NSMutableArray arrayWithObjects:@"武汉",@"上海",@"北京",@"深圳",@"广州",@"重庆",@"香港",@"台海",@"天津",@"天津",@"天津",@"天津",@"黄河",@"黄河", nil];    self.dataList = list;        tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];    // 设置tableView的数据源    tableView.dataSource = self;    // 设置tableView的委托    tableView.delegate = self;    // 设置tableView的背景图    tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background.png"]];    self.myTableView = tableView;    [self.view addSubview:self.myTableView];    }#pragma mark - TableView Methods- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [self.dataList count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellWithIdentifier = @"Cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];    if (cell == nil) {        NSLog(@"%d",indexPath.row);        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];//创建cell,并确定cell样式为UITableViewCellStyleDefault    }    NSUInteger row = [indexPath row];    cell.textLabel.text = [self.dataList objectAtIndex:row];    cell.imageView.image = [UIImage imageNamed:@"2012.jpg"];    cell.detailTextLabel.text = @"详细信息";    return cell;}//点击列表对象- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{        NSString *msg = [[NSString alloc] initWithFormat:@"你选择的是:%@",[self.dataList objectAtIndex:[indexPath row]]];    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"删除", nil];    index = indexPath;  //获取删除的位置,保存为全局变量    [alert show];}//确定对象是否可编辑- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {    return YES;}//定义事件- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    NSLog(@"%@",indexPath);    if (editingStyle == UITableViewCellEditingStyleDelete) {//确定为滑动删除                //先行删除阵列中的物件        [self.dataList removeObjectAtIndex:indexPath.row];                //删除 UITableView 中的物件,并设定动画模式        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];    }    }//添加alert删除事件,- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {           if (buttonIndex == 1) {     // 选择第二个删除按钮            [self.dataList removeObjectAtIndex:index.row];                        //删除 UITableView 中的物件,并设定动画模式            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationFade];        }}@end


0 0
原创粉丝点击