点击UITableViewCell 来回切换内容

来源:互联网 发布:2016年最火的软件 编辑:程序博客网 时间:2024/04/26 14:15



@interface ViewController ()


@property (nonatomic,weak) UITableView *tableView;


@property (nonatomic,strong) NSArray *cellForNoClickedArray;//未点击数据源

@property (nonatomic,strong) NSArray *cellForClickedArray;//点击数据源

@property (nonatomic,strong) NSMutableArray *selectedStatusArray;//被点击状态的修改

@end


@implementation ViewController



-(UITableView *)tableView{


    if (!_tableView) {

     

        UITableView *tb = [[UITableViewalloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height)];

        tb.delegate = self;

        tb.dataSource = self;

        [self.viewaddSubview:tb];


        _tableView = tb;

    }

    

    return_tableView;

}




- (void)viewDidLoad {

    [superviewDidLoad];

   

    [selftableView];

    

    self.cellForNoClickedArray =@[@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"g"];

    self.cellForClickedArray =@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"G"];

    self.selectedStatusArray = [NSMutableArrayarrayWithArray:@[@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0"]];

    

}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    return 60.f;

}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    

    returnself.cellForNoClickedArray.count;

    

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    

    static NSString *listCell =@"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listCell];

    

    if (cell == nil) {

        

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:listCell];

        

    }

    

    

    if ([self.selectedStatusArray[indexPath.row]isEqualToString:@"0"]) {

        

        cell.textLabel.text =self.cellForNoClickedArray[indexPath.row];

        cell.textLabel.textColor = [UIColorredColor];


    }else{

        

        cell.textLabel.text =self.cellForClickedArray[indexPath.row];

        cell.textLabel.textColor = [UIColorgreenColor];


    }

      

    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        

    if([self.selectedStatusArray[indexPath.row]isEqualToString:@"0"]){

        

        [self.selectedStatusArrayreplaceObjectAtIndex:indexPath.rowwithObject:@"1"];

        

    }else{

        

        [self.selectedStatusArrayreplaceObjectAtIndex:indexPath.rowwithObject:@"0"];

    }

    

    [self.tableViewreloadData];

}






0 0
原创粉丝点击