IOS开发基础之英雄展示完整版

IOS开发基础之英雄展示完整版

IOS开发基础之英雄展示完整版_第1张图片

IOS开发基础之英雄展示完整版_第2张图片

//
//  ViewController.m
//  15-英雄展示-单组数据
//
//  Created by 鲁军 on 2021/2/3.
//

#import "ViewController.h"
#import "CZHero.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray *heros;


@property (weak, nonatomic) IBOutlet UITableView *tableView;




@end

@implementation ViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     
    
    //NSLog(@"%ld",(long) indexPath.row);
    

    CZHero *hero = self.heros[indexPath.row];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"编辑英雄" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *conform = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
     
        //NSLog(@"点击了确认按钮");
        NSLog(@"%@",action.title);
    //    action.title
        
      
        
        
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action){
     
        NSLog(@"点击了取消按钮");
    }];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
     
       // textField.placeholder=@"请填写您的反馈信息";
        textField.text=hero.name;
        
    }] ;
    [alert addAction:conform];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}


//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     
//
//    int rowNum = indexPath.row;
//    if(rowNum%2==0){
     
//
//        return 60;
//    }else{
     
//        return 60;
//    }
//
//}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     
    
    CZHero *model =self.heros[indexPath.row];
    
    
   // UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
  
    // 单元格重复使用
    
    //在创建单元格的时候指定一个重用ID
  //  当需要一个新的单元格的时候,先去缓存池中找
    
    
   static NSString *ID = @"hero_cell";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    
    if(cell==nil){
     
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    
    cell.imageView.image=[UIImage imageNamed:model.icon];
    cell.textLabel.text=model.name;
    cell.detailTextLabel.text=model.intro;
    
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    
    NSLog(@"%p----行索引 = %ld",cell,(long)indexPath.row);
    
   // cell.accessoryView =[[UISwitch alloc] init];
//    if(indexPath.row%2==0)
//    {
     
//        cell.backgroundColor=[UIColor yellowColor];
//    }else{
     
//
//        cell.backgroundColor=[UIColor blueColor];
//    }
//    UIView *bgView = [[UIView alloc] init];
//    bgView.backgroundColor = [UIColor greenColor];
//
//    cell.selectedBackgroundView =bgView;
    
    
    
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     
    
    return self.heros.count;
}

//
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     
//    return 1;
//}




- (NSArray *)heros{
     
    
    if(_heros==nil){
     
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
        
        NSArray *arrayDict=[NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *arrayModels = [NSMutableArray array];
        
        
        for(NSDictionary *dict in arrayDict){
     
            CZHero *model = [CZHero heroWithDict:dict];
            [arrayModels addObject:model];
        }
        
        _heros = arrayModels;
    }
    return _heros;
    
}

- (void)viewDidLoad {
     
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
   // self.tableView.rowHeight=80;
    
   // self.tableView.separatorColor=[UIColor redColor];
    
    //self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    
    self.tableView.tableHeaderView=[UIButton buttonWithType:UIButtonTypeContactAdd];
    
    self.tableView.tableFooterView=[[UISwitch alloc] init];
    
}


@end

//
//  CZHero.h
//  15-英雄展示-单组数据
//
//  Created by 鲁军 on 2021/2/3.
//

#import 

NS_ASSUME_NONNULL_BEGIN

@interface CZHero : NSObject

@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *intro;
@property(nonatomic,copy)NSString *name;



-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)heroWithDict:(NSDictionary *)dict;



@end

NS_ASSUME_NONNULL_END

//
//  CZHero.m
//  15-英雄展示-单组数据
//
//  Created by 鲁军 on 2021/2/3.
//

#import "CZHero.h"

@implementation CZHero

-(instancetype)initWithDict:(NSDictionary *)dict{
     
    
    if(self== [super init]){
     
        [self setValuesForKeysWithDictionary:dict];
    }
    return  self;
}
+(instancetype)heroWithDict:(NSDictionary *)dict{
     
    
    return [[self alloc] initWithDict:dict];
}



@end

你可能感兴趣的:(IOS,ios)