Day.03.03 UITableView 表視圖方法


#import "ViewController.h"

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

@interface ViewController ()

@property (nonatomic,strong)NSMutableArray *datalist;

@property (nonatomic,strong)UITableView *tableView;

@property (weak, nonatomic) IBOutlet UITextField *field;

@end

@implementation ViewController
- (IBAction)done:(UIButton *)sender {
    
    
    //將輸入框中的文字轉(zhuǎn)化成數(shù)字
    NSInteger row = [_field.text integerValue];
    
    //通過row 和 section 創(chuàng)建 indexPath
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    
    /**
     *單元格的滑動定位方式 
     
     UITableViewScrollPositionNone,
     UITableViewScrollPositionTop,
     UITableViewScrollPositionMiddle,
     UITableViewScrollPositionBottom

     */
    
    //滑動到指定單元格并附帶動畫
    [_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

//插入新的數(shù)據(jù)
- (IBAction)insert:(UIButton *)sender {
    
    [_datalist insertObject:@"插入的新數(shù)據(jù)!" atIndex:0];
    
    //刷新表視圖
    [_tableView reloadData];
    
    NSLog(@"%@",_datalist);
    
}

//把cells 打印出來
- (IBAction)visibleCells:(UIButton *)sender {
    
    NSArray *cells = [_tableView visibleCells];
    
    NSLog(@"cells:%@",cells);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //背景顏色
    self.view.backgroundColor = [UIColor yellowColor];

    //
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-64) style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    
    //把字體名字放入可變數(shù)組中
    _datalist = [NSMutableArray arrayWithArray:[UIFont familyNames]];
    
    
    /*__________ 表視圖方法 _____________________________________________________*/
    
    
    
    //1.刷新表視圖:當數(shù)據(jù)源發(fā)生更改的時候
    [_tableView reloadData];
    
    //2.獲取當前顯示的cell
//    NSArray *cells = [_tableView visibleCells];
//    
//    NSLog(@"cells:%@",cells);
    
    //2??索引相關
    
    //3.獲取當前顯示的cell的indexpath
    
    NSArray *indexPath = [_tableView indexPathsForVisibleRows];
    
    //4.獲取當前被選中的cell們的索引們
    
//    NSArray *iPs = [_tableView indexPathsForSelectedRows];
    
    //5.獲取內(nèi)容視圖上某個矩形區(qū)域內(nèi)的索引們
    
//    NSArray *iPs = [_tableView indexPathsForRowsInRect:CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)];
    
    //6.獲取指定單元格的索引
//    NSIndexPath *indexPath = [_tableView indexPathForCell:(nonnull UITableViewCell *)];
    
    //7.獲取指定點所在單元格的索引
//    NSIndexPath *ip = [_tableView indexPathForRowAtPoint:(CGPoint)];
    
    //8.獲取選中單元格的索引
    
//    NSIndexPath *sip = [_tableView indexPathForSelectedRow];
    
    //3??
    //9.表視圖滑動到指定的單元格
//    [_tableView scrollToRowAtIndexPath:(nonnull NSIndexPath *) atScrollPosition:(UITableViewScrollPosition) animated:(BOOL)];

}

#pragma mark --UITableViewDataSource

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

    return _datalist.count;

}

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

    static NSString *identy = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];
    }
    
    cell.textLabel.text = [_datalist objectAtIndex:indexPath.row];
    
    return cell;
    
}

@end
屏幕快照 2016-03-03 下午7.40.47.png
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容