IOS開發(fā)UItableView的使用

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


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

    UITableView *_tableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"ACarrotAHoleDemo";
    _titlesArray=@[@"我是第一區(qū)的",@"我是第二區(qū)的"];
    _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"];//設(shè)置背景顏色
    _tableView.separatorColor=[self colorWithHexString:@"#dedede"];//設(shè)置線的顏色
    _tableView.userInteractionEnabled=YES;//設(shè)置tableView可滑動(dòng)(默認(rèn)yes)
    [self.view addSubview:_tableView];
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

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

    return 5;
}
//返回區(qū)數(shù)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _titlesArray.count;
}
//設(shè)置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}
//設(shè)置區(qū)頭高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
//設(shè)置區(qū)尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
//設(shè)置區(qū)頭視圖
-(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;
}
//設(shè)置區(qū)尾視圖
-(nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView *view=[[UIView alloc]init];
    view.backgroundColor=[UIColor clearColor];//透明顏色
    return view;
}
//設(shè)置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設(shè)置為可點(diǎn)擊(默認(rèn)yes)
   // cell.userInteractionEnabled = YES;
   
    cell.textLabel.text=[NSString stringWithFormat:@"我是第%ld區(qū) 第%ld行",indexPath.section,indexPath.row];
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    
   
    return cell;
}
//點(diǎn)擊cell執(zhí)行該方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//反選

}

//根據(jù)UI設(shè)計(jì)師給的顏色值返回出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];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末他去,一起剝皮案震驚了整個(gè)濱河市倒堕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌垦巴,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件秦爆,死亡現(xiàn)場(chǎng)離奇詭異等限,居然都是意外死亡芬膝,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門怒允,熙熙樓的掌柜王于貴愁眉苦臉地迎上來锈遥,“玉大人勘畔,你說我怎么就攤上這事±龌蹋” “怎么了炫七?”我有些...
    開封第一講書人閱讀 164,862評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)钾唬。 經(jīng)常有香客問我万哪,道長(zhǎng),這世上最難降的妖魔是什么抡秆? 我笑而不...
    開封第一講書人閱讀 58,728評(píng)論 1 294
  • 正文 為了忘掉前任奕巍,我火速辦了婚禮,結(jié)果婚禮上儒士,老公的妹妹穿的比我還像新娘的止。我一直安慰自己,他們只是感情好着撩,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著拖叙,像睡著了一般氓润。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上薯鳍,一...
    開封第一講書人閱讀 51,590評(píng)論 1 305
  • 那天咖气,我揣著相機(jī)與錄音,去河邊找鬼挖滤。 笑死采章,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的壶辜。 我是一名探鬼主播,決...
    沈念sama閱讀 40,330評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼担租,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼砸民!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起奋救,我...
    開封第一講書人閱讀 39,244評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤岭参,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后尝艘,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體演侯,經(jīng)...
    沈念sama閱讀 45,693評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評(píng)論 3 336
  • 正文 我和宋清朗相戀三年背亥,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了秒际。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片悬赏。...
    茶點(diǎn)故事閱讀 40,001評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖娄徊,靈堂內(nèi)的尸體忽然破棺而出闽颇,到底是詐尸還是另有隱情,我是刑警寧澤寄锐,帶...
    沈念sama閱讀 35,723評(píng)論 5 346
  • 正文 年R本政府宣布兵多,位于F島的核電站,受9級(jí)特大地震影響橄仆,放射性物質(zhì)發(fā)生泄漏剩膘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評(píng)論 3 330
  • 文/蒙蒙 一盆顾、第九天 我趴在偏房一處隱蔽的房頂上張望怠褐。 院中可真熱鬧,春花似錦椎扬、人聲如沸惫搏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽筐赔。三九已至,卻和暖如春揖铜,著一層夾襖步出監(jiān)牢的瞬間茴丰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工天吓, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留贿肩,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,191評(píng)論 3 370
  • 正文 我出身青樓龄寞,卻偏偏與公主長(zhǎng)得像汰规,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子物邑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評(píng)論 2 355

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