首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS UITableView 去除分組樣式圓角實現矩形列表

     編輯:程式設計綜合問答

問題描述

使用 UITableViewStyleGrouped 建立表格視圖時,預設會顯示圓角分組外觀。若需呈現直角矩形列表(類似 Plain 樣式但保留分區功能),需自訂單元格背景。

解決方案

在 tableView:cellForRowAtIndexPath: 代理方法中,將單元格的 backgroundView 設為空白視圖:

cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];

此舉會移除系統預設的圓角背景,使單元格呈現直角矩形。若需進一步美化,可在 backgroundView 中加入自訂圖片或漸層色塊。

完整代碼範例

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        // 關鍵:移除預設圓角背景
        cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    cell.textLabel.text = [self dataForRowAtIndexPath:indexPath];
    return cell;
}

注意事項

  • 此方法不影響 sectionHeaderView 或 sectionFooterView 的顯示。
  • 若需保留分隔線,請確保 separatorStyle 不為 UITableViewCellSeparatorStyleNone。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved