zoukankan      html  css  js  c++  java
  • iOS

    #import <UIKit/UIKit.h>
    
    @interface TestCell : UITableViewCell
    @property(nonatomic,copy)NSString *content;
    /**
     *  标记行是否被选中
     */
    @property(nonatomic,assign)BOOL isChecked;
    @end
    #import "TestCell.h"
    @interface TestCell()
    @property (weak, nonatomic) IBOutlet UIImageView *mark_ImageView;
    @property (weak, nonatomic) IBOutlet UILabel *firstLabel;
    
    @end
    
    @implementation TestCell
    
    - (void)awakeFromNib
    {
        self.mark_ImageView.hidden = YES;
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
       
    }
    -(void)setContent:(NSString *)content
    {
        if (_content!=content) {
            _firstLabel.text=content;
        }
    
    }
    /**
     *  这里只是用到了一张图所以通过来imageview的hidden来显示是否选中
     *
     *  @param isChecked
     */
    - (void)setIsChecked:(BOOL)isChecked
    {
        _isChecked = isChecked;
        //选中
        if (_isChecked) {
            self.mark_ImageView.hidden = NO;
            self.mark_ImageView.image = [UIImage imageNamed:@"mark_select"];
        }
        else
        {
            self.mark_ImageView.hidden = YES;
        }
    
    }
    
    @end
    
    
    
    
    #import "ViewController.h"
    #import "TestCell.h"
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    
    {
       NSIndexPath *selectIndexPath;
    
    }
    
    @property(nonatomic,strong)NSMutableArray *dataArray;
    
    /**
     *  保存选中行的对应的数据
     */
    @property(nonatomic,strong)NSMutableArray *markArray;
    
    
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
      
        for (NSInteger i=0; i<20; i++) {
            [self.dataArray addObject:[NSString stringWithFormat:@"选中第%ld行",i]];
        }
        
        
        
        
        
    }
    
    - (NSMutableArray *)dataArray{
    
        if (!_dataArray) {
            _dataArray =[NSMutableArray array];
        }
        return _dataArray;
        
    }
    
    - (NSMutableArray *)markArray{
        
        if (!_markArray) {
            _markArray =[NSMutableArray array];
        }
        return _markArray;
        
    }
    
    
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return  self.dataArray.count;
    }
    static NSString *cellIdentifier = @"TestCell";
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        TestCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        cell.content= self.dataArray[indexPath.row];
        
        if (selectIndexPath==indexPath) {
            cell.isChecked = YES;
        }
        else{
            cell.isChecked = NO;
        }
        
        
        return cell;
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 44.0f;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //未选中
       TestCell *noCheckedCell = (TestCell *)[tableView cellForRowAtIndexPath:selectIndexPath];
         noCheckedCell.isChecked = NO;
        selectIndexPath = indexPath;
        
        //选中
        TestCell *checkedCell = (TestCell *)[tableView cellForRowAtIndexPath:indexPath];
        checkedCell.isChecked = YES;
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        //刷新指定的行
        NSIndexPath *indexPath_Row=[NSIndexPath indexPathForRow:indexPath.row inSection:0];
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath_Row,nil] withRowAnimation:UITableViewRowAnimationNone];
       
        NSString *checkString  = self.dataArray[indexPath.row];
        if ([self.markArray containsObject:checkString]) {
             [self.markArray removeObject:checkString];
        }
        else{
            [self.markArray removeAllObjects];
            [self.markArray addObject:checkString];
        }
        
        NSLog(@"self.checkArray =%@",self.markArray);
        
        
        
    }
    

      

  • 相关阅读:
    二读《活着》有感
    Linux系统上安装JDK和Tomcat服务器
    在阿里云服务器上安装完成并启动Tomcat后,通过http不能访问--解决办法
    安装JDK出现错误:-bash: /usr/java/jdk1.7.0_71/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory解决办法
    python结巴(jieba)分词
    mysql数据库优化
    ansible常见模块
    CentOS6.5 64位下安装部署Ansible
    [Python] 利用commands模块执行Linux shell命令
    python迭代器
  • 原文地址:https://www.cnblogs.com/thbbsky/p/5811126.html
Copyright © 2011-2022 走看看