对话框和table的使用

1,UICVIewControl的生命周期

-(void)viewWillAppear:(BOOL)animated{ //将要显示
    NSLog(@"viewWillAppear....");
    
}

-(void)viewDidAppear:(BOOL)animated{
    NSLog(@"viewDidAppear..."); 显示完成
}


-(void)viewWillDisappear:(BOOL)animated{
 NSLog(@"viewWillDisappear..."); 将要关闭
}

-(void)viewDidDisappear:(BOOL)animated{
    
    NSLog(@"viewDidDisappear..");关闭完成
}

 界面显示

2015-12-25 00:07:40.884 viewDemo[517:8149] viewWillAppear....

2015-12-25 00:07:40.927 viewDemo[517:8149] viewDidAppear...

 

跳转到下一个界面

2015-12-25 00:08:41.575 viewDemo[517:8149] viewWillDisappear...

2015-12-25 00:08:42.111 viewDemo[517:8149] viewDidDisappear..

 

 

2,对话框

 

UIAlertView方式创建

 

//    添加警告框
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"确定删除" message:@"删除" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"no", nil];
    
    UIActivityIndicatorView *act=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    [alert show];
    [alert release];

 

UIalertControl创建对话框

  
    NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
    NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
    NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);

    
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    
    //设置Action
    UIAlertAction *actionleft=[UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");

    
        
    }];
    
    
    UIAlertAction *actionright=[UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
    }];
    
    
    //设置输入框
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.text=@"代号";
        
    }];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.text=@"姓名";
    }];
    
    
    
    //将事件添加到UIAlertController
    [alert addAction:actionleft];
    [alert addAction:actionright];
    
    //
    [self presentViewController:alert animated:1 completion:nil];
    //释放对象
//    [actionleft release];
//    [actionright release];
//    [alert release];

 

创建底部对话框

    //创建对话款
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"保存成功" message:@"是否继续" preferredStyle:UIAlertControllerStyleActionSheet];
    //添加按钮
    [alert addAction:[UIAlertAction actionWithTitle:@"停留本页" style:UIAlertActionStyleCancel handler:nil]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
       
    }]];
    
    //显示对话款
    [self presentViewController:alert animated:YES completion:nil];

 

3,TableView

@interface TableViewController (){

    UITableView *table;
}

@property(strong,nonatomic)NSMutableArray *arrayList1; //数据源
@property (strong,nonatomic)NSMutableArray *arrayTitle;//标题
@property(strong,nonatomic)NSMutableArray *arrayList2;
@property(strong,nonatomic)NSDictionary *array;

@end

@implementation TableViewController

@synthesize arrayList1;
@synthesize arrayTitle;
@synthesize arrayList2;
@synthesize array;



- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
//     self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    table=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 430, 600)];
    
    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cqe.jpg"]];
    //
    table.dataSource=self;
    table.delegate=self;
    
    if (arrayList1==nil) {
        arrayList1 =[[NSMutableArray alloc]initWithObjects:@"美元/日元",@"美元/韩元",@"韩元/日元",@"港币/日元",@"美元/欧元",@"美元/英镑",@"美元/新加坡元",@"美元/澳元",@"美元/卢布",@"泰铢/日元",@"英镑/泰铢",@"新加坡元/澳门元",@"美元/港币",@"港币/台币",@"台币/越南盾",@"美元/韩元", nil];
        arrayTitle=[[NSMutableArray alloc]initWithObjects:@"自选",@"交叉", nil];
        
        arrayList2=[[NSMutableArray alloc]initWithObjects:@"美元/港币",@"韩元/日元", nil];
        
        array=@{[arrayTitle objectAtIndex:0]:arrayList1, [arrayTitle objectAtIndex:1]:arrayList2};
    }
    
    
    
  
}

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


//设置标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return [arrayTitle objectAtIndex:section];
}

//设置尾部的标题
-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    return  @"这是一个简单的使用";
}

//设置头部的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 20;
}


//设置底部的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 100;
}

//自定义头部View的样式
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];
    
    [view setBackgroundColor:[UIColor brownColor]];//改变标题的颜色,也可用图片
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 40)];
    
    label.textColor = [UIColor redColor];
    
    label.backgroundColor = [UIColor clearColor];
    
    label.text = [arrayTitle objectAtIndex:section];
    
    [view addSubview:label];
    
    return view;
}



//设置底部View样式
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIImageView * v1=[[UIImageView alloc]init];
    v1.image=[UIImage imageNamed:@"bk.jpg"];
    v1.frame=CGRectMake(0, 0, 320, 100);
    return v1;

}


#pragma mark - Table view data source
//指定多少分区,根据arrayTitle计算
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return [arrayTitle count];
}

//设置数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    //1,根据当前点击的区域设置数据
    switch (section) {
        case 0:
            
            return [arrayList1 count];
        case 1:
            return [arrayList2 count];
            
        default:
            break;
    }
    
    
    return 0;
    
}

//绘制数据  cellForRowAtINdexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    static NSString * string = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if(cell ==  nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    
    //根据分区操作
    switch (indexPath.section) {
        case 0:
            [[cell textLabel] setText:[arrayList1 objectAtIndex:indexPath.row]];
            break;
            
        case 1:
            [[cell textLabel] setText:[arrayList2 objectAtIndex:indexPath.row]];
            
            break;
        default:
            break;
    }
    
    return cell;
}



// Override to support conditional editing of the table view. 滑动是否出现删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}



// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    
    
}



// Override to support conditional rearranging of the 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}



#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


//设置行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 3;
}

//点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [table deselectRowAtIndexPath:indexPath animated:1];//设置选中后颜色消失
    
    //获得选中的cell
    
//   UITableViewCell *cell= [table cellForRowAtIndexPath:indexPath];
////    添加警告框
//    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"确定删除" message:@"删除" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"no", nil];
//    
//    UIActivityIndicatorView *act=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
//    
//    [alert show];
//    [alert release];
    
    
    [self setAlertControlLrean];
    
}


//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    return [self.array allKeys];
}


//获得选中的行
-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSUInteger index=[indexPath row];
    if (index==0) {
        return nil;
    }
    return indexPath;
}


//获得对话框的点击位置
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSLog(@"%ld",buttonIndex);

}

//新对话框的使用

-(void)setAlertControlLrean{
    
    
    NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
    NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
    NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);

    
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    
    //设置Action
    UIAlertAction *actionleft=[UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");

    
        
    }];
    
    
    UIAlertAction *actionright=[UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
    }];
    
    
    //设置输入框
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.text=@"代号";
        
    }];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.text=@"姓名";
    }];
    
    
    
    //将事件添加到UIAlertController
    [alert addAction:actionleft];
    [alert addAction:actionright];
    
    //
    [self presentViewController:alert animated:1 completion:nil];
    //释放对象
//    [actionleft release];
//    [actionright release];
//    [alert release];


}


@end

 

猜你喜欢

转载自baihe747.iteye.com/blog/2266344