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的樣式來滿足需求!