iOS UITableView中cell的自帶樣式

UITableView是iOS開發(fā)中用的非常多的一個控件,UITableview用來展示列表數(shù)據(jù),相當(dāng)于安卓中的listview。不同于安卓中的listview自定義item來定義列表顯示的樣式,iOS中系統(tǒng)自帶了四種樣式在很多場合都夠我們使用了。

系統(tǒng)自帶的UITableView樣式有兩種:

  • UITableViewStylePlain:(展示單組數(shù)據(jù),沒有分組)
UITableViewStylePlain.png
  • UITableViewStyleGrouped:(展示分組數(shù)據(jù))
UITableViewStyleGrouped.png

系統(tǒng)自帶的UITableViewCell樣式有4種:

  • UITableViewCellStyleDefault:
    • Default樣式:左邊有一個顯示圖片的imageView和一個標(biāo)題textLabel。
    • 上面是頭布局顯示一張圖片(可做成大部分APP中下拉時圖片有拉伸且粘在頂部的效果)
    • 使用代碼:為了提高效率我直接對字典取值顯示(下面都是)
static NSString * cellID=@"cellID";
   UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
   if (cell==nil) {
       cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
   }
   cell.textLabel.text = self.dataArr[indexPath.row][@"title"];
   cell.imageView.image = [UIImage imageNamed:@"test_list"];
//cell右側(cè)的小箭頭
   //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   return cell;
UITableViewCellStyleDefault.png
  • UITableViewCellStyleSubtitle:
    • Subtitle樣式:左邊還是一個顯示圖片的imageView,不同的是上邊有一個主標(biāo)題textLabel和一個副標(biāo)題detailTextLabel。主標(biāo)題字體大且加黑,副標(biāo)題字體小在主標(biāo)題下邊。
    • 代碼使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID=@"cellID";
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    cell.textLabel.text = self.dataArr[indexPath.row][@"Name"];
    cell.detailTextLabel.text = self.dataArr[indexPath.row][@"Others1"];
    cell.imageView.image = [UIImage imageNamed:@"music"];
    cell.backgroundColor = LightWrite;
    return cell;
}
UITableViewCellStyleSubtitle.png
  • UITableViewCellStyleValue1:
    • Value1樣式:左邊顯示圖片的imageView和一個主標(biāo)題textLabel,右邊一個副標(biāo)題detailTextLabel。
    • 代碼使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID=@"cellID";
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    cell.textLabel.text = self.dataArr[indexPath.row][@"Name"];
    cell.detailTextLabel.text = self.dataArr[indexPath.row][@"Others1"];
    cell.imageView.image = [UIImage imageNamed:@"music"];
    cell.backgroundColor = LightWrite;
    return cell;
}
UITableViewCellStyleValue1.png
  • UITableViewCellStyleValue2:
    • Value2樣式:左邊一個主標(biāo)題textLabel字體偏小,右邊一個副標(biāo)題detailTextLabel。
  • imageView可選(顯示在最左邊)
  • 代碼使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID=@"cellID";
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellID];
    }
    cell.textLabel.text = self.dataArr[indexPath.row][@"Name"];
    cell.detailTextLabel.text = self.dataArr[indexPath.row][@"Others1"];
    cell.imageView.image = [UIImage imageNamed:@"music"];
    cell.backgroundColor = LightWrite;
    return cell;
}
UITableViewCellStyleValue2.png

當(dāng)然如果如果你想要在每個cell的右側(cè)添加一個小箭頭來提示用戶cell可點(diǎn) 只需要加上這句代碼即可

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

雖然系統(tǒng)自帶了4種樣式,不過平時開發(fā)中更多的還是自定義cell的樣式來滿足需求!

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

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

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