Day.03.03 UITableView 定制單元格

#import "ViewController.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSMutableArray *datalist;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-64) style:UITableViewStyleGrouped];
    
    _tableView.dataSource = self;
    
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    
    //1.獲取文件路徑
    NSString *path = [[NSBundle mainBundle]pathForResource:@"font" ofType:@"plist"];
    
    //2.通過路徑加載容器對象
    
//    _datalist = [NSMutableArray arrayWithArray:[NSArray arrayWithContentsOfFile:path]];
    
    //(1)讀取文件不可變數(shù)組
    NSArray *plist = [NSArray arrayWithContentsOfFile:path];
    
    //(2)初始化可變數(shù)組
    _datalist = [[NSMutableArray alloc]init];
    
    //(3)將所有的二級數(shù)組復制為可變數(shù)組 并添加到datalist中
    for (NSArray *subarr in plist) {
        
        NSMutableArray *ma = [NSMutableArray arrayWithArray:subarr];
        
        [_datalist addObject:ma];
        
    }
    
    
}

//進入多選模式
- (IBAction)multipleSelect:(UIButton *)sender {
    
    
    if (sender.selected == YES) {
        
        
        //1.獲取所有選中的單元格下標
        NSArray *indexPaths = [_tableView indexPathsForSelectedRows];
        
        //2.刪除數(shù)據(jù)
        /**
         *  為了防止下標越界,所以采用倒序遍歷并刪除
         */
        
        NSLog(@"%@",indexPaths);
        
        for (NSInteger i = indexPaths.count-1; i>=0; i--) {
            
            NSIndexPath *indexP = indexPaths[i];
            
            //(1)獲取section對應的數(shù)組
            NSMutableArray *subArr = _datalist[indexP.section];
            
            //(2)
            [subArr removeObjectAtIndex:indexP.row];
            
        }
        
        //3.刪除單元格
        [_tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    }
    
    
    sender.selected = !sender.selected;
    
    //在編輯期間允許單元格多選 --> 開啟該選項 插入單元格和刪除單個單元格 功能就不存在了
    _tableView.allowsMultipleSelectionDuringEditing = sender.selected;
    
    //表視圖開啟編輯模式
    [_tableView setEditing:sender.selected animated:YES];
    
    NSLog(@"%@",_datalist);
    
}

#pragma mark --UITableViewDataSource

//可選方法:返回 組個數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return _datalist.count;//組個數(shù)
}

//返回 每個組有多少個單元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    //1.從datalist中獲取section下標對應的對象--->小數(shù)組(盛放的NSString*)
    NSArray *subArray = _datalist[section];
    
    return subArray.count;

}

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

    //1.
    static NSString *identifier = @"font_cell";
    
    //2.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    //3.
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    
    
    //1.不分組情況
    
//    cell.textLabel.text = [_dataList objectAtIndex:indexPath.row];
//    
//    cell.textLabel.font = [UIFont fontWithName:[_dataList objectAtIndex:indexPath.row] size:20];
    
    //2.分組
    
    
        //(1)在datalist中 找到section對應的二級數(shù)組
    
    NSArray *subArray = _datalist[indexPath.section];
    
        //(2)在二級數(shù)組中 找到row對應的字符串對象
    cell.textLabel.text = subArray[indexPath.row];
    cell.textLabel.font = [UIFont fontWithName:subArray[indexPath.row] size:20];

    return cell;
}

@end

屏幕快照 2016-03-03 下午8.53.32.png
屏幕快照 2016-03-03 下午8.53.21.png
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容