有没有办法从UITableView中的单个单元格中删除分隔线?

时间:2009-07-31 15:19:17

标签: iphone uitableview

我知道我可以将UITableView属性separatorStyle更改为UITableViewCellSeparatorStyleNone或UITableViewCellSeparatorStyleSingleLine,以便以某种方式更改TableView中的所有单元格。

我感兴趣的是一些单元格带有SingleLine Separator而一些单元格没有。这可能吗?

7 个答案:

答案 0 :(得分:16)

您最好的选择可能是将表格separatorStyle设置为UITableViewCellSeparatorStyleNone并在需要时手动添加/绘制一条线(可能在tableView:cellForRowAtIndexPath:中)。

答案 1 :(得分:9)

根据迈克的建议,这就是我所做的。

在tableView中:cellForRowAtIndexPath:

...
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    // Drawing our own separatorLine here because I need to turn it off for the
    // last row. I can only do that on the tableView and on on specific cells.
    // The y position below has to be 1 less than the cell height to keep it from
    // disappearing when the tableView is scrolled.
    UIImageView *separatorLine = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, cell.frame.size.height - 1.0f, cell.frame.size.width, 1.0f)];
    separatorLine.image = [[UIImage imageNamed:@"grayDot"] stretchableImageWithLeftCapWidth:1 topCapHeight:0];
    separatorLine.tag = 4;

    [cell.contentView addSubview:separatorLine];

    [separatorLine release];
}

// Setup default cell setttings.
...
UIImageView *separatorLine = (UIImageView *)[cell viewWithTag:4];
separatorLine.hidden = NO;
...
// In the cell I want to hide the line, I just hide it.
seperatorLine.hidden = YES;
...

在viewDidLoad中:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

答案 2 :(得分:6)

这很适合我。

 cell.separatorInset = UIEdgeInsetsMake(0, 160, 0, 160);

所有这一切都是将线的左边和右边的插图推到死点,即160,这使它看不见。

然后您可以通过indexPath.row;

控制应用它的单元格

答案 3 :(得分:5)

self.separatorInset = UIEdgeInsetsMake(0, CGRectGetWidth(self.frame)/2, 0, CGRectGetWidth(self.frame)/2);

答案 4 :(得分:2)

我能够通过使左边插入整个单元格边界宽度的大小来隐藏多个不同单元格的分隔线。替换" indexPath.row == 0"使用删除分隔线所需的行号。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

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


if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 8 || indexPath.row == 9) {
    cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);
}
else {
    cell.separatorInset = UIEdgeInsetsMake(0, 24, 0, 0);
}


return cell;
}

答案 5 :(得分:1)

对于具有相同问题且使用swift且想要仅为特定类型的单元格隐藏分隔线的人,可以采用以下方法:

 override func layoutSubviews() {
    super.layoutSubviews()
    subviews.forEach { (view) in
        if view.dynamicType.description() == "_UITableViewCellSeparatorView" {
            view.hidden = true
        }
    }
}

答案 6 :(得分:0)

实现此目的的最佳方法是关闭默认行分隔符,子类def getStoredAccessToken(authInfo: AuthInfo[User]) = { println(authInfo.user.email+" ---- "+authInfo.user._id.get) var future = accessTokenService.findOne($doc("clientId" $eq authInfo.user.email, "userId" $eq authInfo.user._id.get)); //user findOne instead of findRandom in JsonDao future.map { option => { if (!option.isEmpty){ var accessToken = option.get; var value = Crypto.validateToken(accessToken.createdAt.value) Some(scalaoauth2.provider.AccessToken(accessToken.accessToken, accessToken.refreshToken, authInfo.scope, Some(value), new Date(accessToken.createdAt.value))) }else{ Option.empty } }} 并添加自定义行分隔符作为UITableViewCell的子视图 - 请参阅下面用于呈现的自定义单元格类型为contentView的对象,它具有两个字符串属性SNStockticker

name

要禁用默认行分隔符,请import UIKit private let kSNStockCellCellHeight: CGFloat = 65.0 private let kSNStockCellCellLineSeparatorHorizontalPaddingRatio: CGFloat = 0.03 private let kSNStockCellCellLineSeparatorBackgroundColorAlpha: CGFloat = 0.3 private let kSNStockCellCellLineSeparatorHeight: CGFloat = 1 class SNStockCell: UITableViewCell { private let primaryTextColor: UIColor private let secondaryTextColor: UIColor private let customLineSeparatorView: UIView var showsCustomLineSeparator: Bool { get { return !customLineSeparatorView.hidden } set(showsCustomLineSeparator) { customLineSeparatorView.hidden = !showsCustomLineSeparator } } var customLineSeparatorColor: UIColor? { get { return customLineSeparatorView.backgroundColor } set(customLineSeparatorColor) { customLineSeparatorView.backgroundColor = customLineSeparatorColor?.colorWithAlphaComponent(kSNStockCellCellLineSeparatorBackgroundColorAlpha) } } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } init(reuseIdentifier: String, primaryTextColor: UIColor, secondaryTextColor: UIColor) { self.primaryTextColor = primaryTextColor self.secondaryTextColor = secondaryTextColor self.customLineSeparatorView = UIView(frame:CGRectZero) super.init(style: UITableViewCellStyle.Subtitle, reuseIdentifier:reuseIdentifier) selectionStyle = UITableViewCellSelectionStyle.None backgroundColor = UIColor.clearColor() contentView.addSubview(customLineSeparatorView) customLineSeparatorView.hidden = true } override func prepareForReuse() { super.prepareForReuse() self.showsCustomLineSeparator = false } // MARK: Layout override func layoutSubviews() { super.layoutSubviews() layoutCustomLineSeparator() } private func layoutCustomLineSeparator() { let horizontalPadding: CGFloat = bounds.width * kSNStockCellCellLineSeparatorHorizontalPaddingRatio let lineSeparatorWidth: CGFloat = bounds.width - horizontalPadding * 2; customLineSeparatorView.frame = CGRectMake(horizontalPadding, kSNStockCellCellHeight - kSNStockCellCellLineSeparatorHeight, lineSeparatorWidth, kSNStockCellCellLineSeparatorHeight) } // MARK: Public Class API class func cellHeight() -> CGFloat { return kSNStockCellCellHeight } // MARK: Public API func configureWithStock(stock: SNStock) { textLabel!.text = stock.ticker as String textLabel!.textColor = primaryTextColor detailTextLabel!.text = stock.name as String detailTextLabel!.textColor = secondaryTextColor setNeedsLayout() } } 。消费者方面相对简单,见下面的例子:

tableView.separatorStyle = UITableViewCellSeparatorStyle.None;