簡體   English   中英

UITableViewCellAccessoryCheckmark 覆蓋 iOS 7 上的單元格分隔符

[英]UITableViewCellAccessoryCheckmark covers cell separator on iOS 7

在 iOS 7 上使用此代碼會導致分隔符視圖被覆蓋或縮短:

cell.accessoryType = UITableViewCellAccessoryCheckmark;

如何修復分隔符視圖?

我正在使用原型單元,但我沒有對它們進行子類化。

在此處輸入圖片說明

[編輯]

這是來自 cellForRowAtIndexPath 的相關代碼:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

if (indexPath.section == kDefaultViewSection){
    NSArray *defaultViewNames = @[LQSettingsSentenceView, LQSettingsFullTextView, LQSettingsFlashcardsView];
    NSString *preferredViewName = [LQSettings valueForKey:LQSettingsPreferredLessonView];
    if ([defaultViewNames[indexPath.row] isEqualToString:preferredViewName]){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section != kDefaultViewSection){
        return;
    }

    // Just turn all checks off for a minute
    for (int x=0; x<3; x++) {
        NSIndexPath *ip = [NSIndexPath indexPathForRow:x inSection:kDefaultViewSection];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:ip];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;    
}

就我而言,我禁用了系統提供的分隔線,並在單元格的 xib 中添加了我的自定義分隔線。有時,正確的復選標記覆蓋了底部分隔線的一部分。我為解決問題所做的是更改了尾隨約束到cell而不是contentView [不是超級視圖]

在此處輸入圖片說明

在表格視圖中禁用分隔符並在單元格底部添加子視圖自定義分隔符視圖。

您可以在 UITableViewCell 的backgroundView屬性上設置backgroundView 在該自定義 backgroundView 中添加一個分隔線視圖,擴展視圖的整個寬度。 backgroundView 不受附件視圖的影響。

打開 var backgroundView: UIView?

UITableViewStylePlain 中的單元格默認為 nil,UITableViewStyleGrouped 中的默認值為非 nil。 'backgroundView' 將被添加為所有其他視圖后面的子視圖。

例如...

let separator = UIView()
separator.backgroundColor = .gray

let customBackgroundView = UIView()

customBackgroundView.addSubview(separator)
separator.translatesAutoresizingMaskIntoConstraints = false

separator.bottomAnchor.constraint(equalTo: customBackgroundView.bottomAnchor).isActive = true
separator.widthAnchor.constraint(equalTo: customBackgroundView.widthAnchor).isActive = true
separator.heightAnchor.constraint(equalToConstant: 1).isActive = true

backgroundView = customBackgroundView

沒有一個答案有幫助。 我結束了:

  1. 使用 ✓ 作為 UILabel 為 UITableViewCell 創建自定義類。 您也可以使用 UIImage。
  2. 將IBOutlet拖到自定義類中,我命名為checkMarkLabel
  3. 設置約束。
  4. 在主 viewController 類中,我聲明了一個字符串屬性selectedItem
  5. cellforrow函數中,

     if selectedItem == tableData[indexPath.tow]{ cell.checkMarkLabel.isHidden = false } else { cell.checkMarkLabel.isHidden = true }
  6. didSelectRow函數中,

     selectedItem = tableData[indexPath.tow] DispatchQueue.main.async { self.tableView.reloadRows(at: tableView.indexPathsForVisibleRows!, with: .none) }

轉到 Xib 或 Storyboard,選擇 tableview 並將分隔符樣式設置為單行。快照

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM