UITableview

 

#import "DengViewController.h"
#import <CoreGraphics/CoreGraphics.h>
@interface DengViewController ()



@end

@implementation DengViewController

@synthesize arrayStrig=_arrayStrig;
//@synthesize number=_number;
//@synthesize data=_data;
//@synthesize tableview=_tableview;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _data=[[NSMutableArray alloc] initWithCapacity:0];
    }
    
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    /*
    UITableView *tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    tableview.delegate=self;
    tableview.dataSource=self;
    [tableview setEditing:YES];
        [self.view addSubview:tableview];
    
    NSArray *array1=[[NSArray alloc] initWithObjects:@"one",@"three",@"five",@"seven",@"nine", nil];
    NSArray *array2=[[NSArray alloc] initWithObjects:@"two",@"four",@"six",@"eight",@"ten" ,nil];
    
    self.arrayStrig=[[NSArray alloc] initWithObjects:array1,array2, nil];
     */
    
    UIView *v =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    v.backgroundColor=[UIColor grayColor];
    [self.view addSubview:v];
    
    UIButton *but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    but.frame=CGRectMake(20, 5, 40, 30);
    [but setTitle:@"添加" forState:UIControlStateNormal];
    [but addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    but.alpha=0.8;
    [v addSubview:but];
    
    but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    but.frame=CGRectMake(260, 5, 40, 30);
    [but setTitle:@"编辑" forState:UIControlStateNormal];
    [but addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
    but.alpha=0.8;
    [v addSubview:but];
    
    CGRect frame= CGRectMake(0, 40, self.view.bounds.size.width, self.view.bounds.size.height-40);
    _tableview=[[UITableView alloc] initWithFrame:frame];
    _tableview.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
    _tableview.delegate=self;
    _tableview.dataSource=self;
    [self.view addSubview:_tableview];
    
   // _data=[[NSMutableArray alloc] initWithObjects:@"one",@"three",@"five",@"seven",@"nine", nil];
    
    // Do any additional setup after loading the view, typically from a nib.
}


#pragma mark-(处理tableview)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
  
    //return [self.arrayStrig count];
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
#if 0
    if (0==section) {
        return 5;
    }else
        return 5;
#endif
    
   // return [[self.arrayStrig objectAtIndex:section] count];
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
    NSInteger row=indexPath.row;
    NSInteger section=indexPath.section;
    NSString *data = [[self.arrayStrig objectAtIndex:section] objectAtIndex:row];
    
#pragma mark可重用cell机制
    static NSString * identifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    cell.textLabel.text=data;
    
    return cell;
}
#if 0
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section==1) {
        return @"偶数";
    }else
    return @"奇数";
}
#endif

#pragma mark-自定义页眉
/*
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    label.backgroundColor=[UIColor grayColor];
    label.textAlignment=NSTextAlignmentLeft;
    label.font=[UIFont fontWithName:nil size:18];
    label.textColor=[UIColor redColor];
    //label.font=[UIFont systemFontOfSize:18];
    if (section==1) {
         label.text=@"偶数";
       
    
    }else
        label.text=@"奇数";
    

   return label;
    
}
 */
#pragma mark-自定义页脚

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
    label.backgroundColor=[UIColor orangeColor];
    label.textAlignment=NSTextAlignmentCenter;
    label.text=@"尾页";
    label.font=[UIFont fontWithName:nil size:15];
    return label;
}


#pragma mark-选择section

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger row=indexPath.row;
    NSInteger section=indexPath.section;
#pragma mark自定义警告视图的messag
    NSString *msg = [NSString stringWithFormat:@"section:%d,row:%d",section,row];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
}

#pragma mark-索引
/*
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"%c",'A'+section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
    
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    return index;
}
*/
#pragma mark-可编辑

- (void)add:(UIButton *)but{
    NSString *string = [NSString stringWithFormat:@"%d",_number++];
    [_data addObject:string];
    [_tableview reloadData];
   
}

- (void)edit:(UIButton *)but{
    if ([_tableview isEditing]) {
        [but setTitle:@"编辑" forState:UIControlStateNormal];
        [_tableview setEditing:NO animated:YES];
    }else{
        [but setTitle:@"完成" forState:UIControlStateNormal];
        [_tableview setEditing:YES animated:YES];
    }
}

#pragma mark选择编辑的风格
- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row%2) {
        return UITableViewCellEditingStyleDelete;
    }else
        return UITableViewCellEditingStyleInsert;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        [_data removeObjectAtIndex:indexPath.row];
        [_tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
        
    }
    else if (editingStyle==UITableViewCellEditingStyleInsert){
        [_data insertObject:[NSString stringWithFormat:@"%d",_number++] atIndex:indexPath.row];
        [_tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

posted @ 2015-06-17 10:42  D飞翔的猫  阅读(130)  评论(0编辑  收藏  举报