IOS開發UItableView的使用

在我們的開發中會有很多地方會用到表(UItableView)的使用,下面我就簡單講一下常用的


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *_titlesArray;

    UITableView *_tableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"ACarrotAHoleDemo";
    _titlesArray=@[@"我是第一區的",@"我是第二區的"];
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.backgroundColor=[self colorWithHexString:@"#eeeeee"];//設置背景顏色
    _tableView.separatorColor=[self colorWithHexString:@"#dedede"];//設置線的顏色
    _tableView.userInteractionEnabled=YES;//設置tableView可滑動(默認yes)
    [self.view addSubview:_tableView];
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

//返回行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 5;
}
//返回區數
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _titlesArray.count;
}
//設置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}
//設置區頭高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
//設置區尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
//設置區頭視圖
-(nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]init];
    view.backgroundColor=[UIColor clearColor];//透明顏色
    UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    lbl.text=_titlesArray[section];
    lbl.textAlignment=NSTextAlignmentCenter;
    lbl.textColor=[self colorWithHexString:@"#2c2c2c"];
    [view addSubview:lbl];
    
    return view;
}
//設置區尾視圖
-(nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView *view=[[UIView alloc]init];
    view.backgroundColor=[UIColor clearColor];//透明顏色
    return view;
}
//設置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *str=@"indexpath";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    //將cell設置為可點擊(默認yes)
   // cell.userInteractionEnabled = YES;
   
    cell.textLabel.text=[NSString stringWithFormat:@"我是第%ld區 第%ld行",indexPath.section,indexPath.row];
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    
   
    return cell;
}
//點擊cell執行該方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//反選

}

//根據UI設計師給的顏色值返回出UIColor
-(UIColor *) colorWithHexString: (NSString *)color{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if ([cString length] <6) {
        return [UIColor clearColor];
    }
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] !=6)
        return [UIColor clearColor];
    NSRange range;
    range.location =0;
    range.length =2;
    NSString *rString = [cString substringWithRange:range];
    range.location =2;
    NSString *gString = [cString substringWithRange:range];
    range.location =4;
    NSString *bString = [cString substringWithRange:range];
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float) r /255.0f) green:((float) g /255.0f) blue:((float) b /255.0f) alpha:1.0f];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容