2012-08-15 55 views
3

我嘗試在我的UITableView中實現我自己的簡單樣式的單元格,並且對分隔符有問題。正常工作效果很好,但是當我選擇一個單元格時,它會消失。我嘗試添加到我的customSelect視圖分隔符,但隨後我無法在任何地方看到分隔符。我如何添加分隔符到選定的單元格?UITableViewCell和消失的分隔符

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyCellIdentifier = @"MyCellIdentifier"; 

    UITableViewCell *cell = [wallMenuTableView dequeueReusableCellWithIdentifier:MyCellIdentifier]; 

    if(cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier]; 

     MenuItemModel *mItem = [menu.menuPositions objectAtIndex:indexPath.row]; 
     cell.textLabel.text = mItem.displayName; 
     cell.textLabel.textColor = [UIColor colorWithRed:0.70 green:0.70 blue:0.70 alpha:1.0]; 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 
     cell.textLabel.font = [UIFont fontWithName:@"ArialMT" size:16]; 
     cell.textLabel.shadowColor = [UIColor blackColor]; 
     cell.textLabel.shadowOffset = CGSizeMake(0.0, 1.0); 

     customSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, (cell.frame.origin.y), 320, 2)]; 
     customSeparator.backgroundColor=[UIColor blackColor]; 
     [customSeparator.layer setShadowOffset:CGSizeMake(0.0, 0.8)]; 
     [customSeparator.layer setShadowOpacity:0.8]; 
     [customSeparator.layer setShadowRadius:0.8]; 
     [customSeparator.layer setShadowColor:[UIColor grayColor].CGColor]; 
     [cell.contentView addSubview:customSeparator]; 

     customSelect = [[UIView alloc] initWithFrame:CGRectMake(0, (cell.frame.origin.y+2), cell.frame.size.width, cell.frame.size.height)]; 
     //[customSelect addSubview:customSeparator]; 
     customSelect.backgroundColor = [UIColor clearColor]; 
     [cell setSelectedBackgroundView:customSelect]; 

    } 

    return cell; 
} 

和電流結果:

enter image description here

+0

嘗試移動if(cell == nil)塊之外的分隔符創建代碼 – 2012-08-15 14:51:33

+0

@Rickay - 沒有工作。 – Kuba 2012-08-15 14:53:49

回答

7

使用UIImageView而不是簡單的UIView作爲分隔符。設置圖像值(這很重要!)而不是backgroundColor值,並用縮放來填充圖像。

+0

謝謝..這麼簡單.. :) – Kuba 2012-09-06 08:55:38

+0

太棒了!它爲我工作! – 2012-09-06 10:10:42

0

也許你costumSelect是Cell的內容查看下。我之前實現了這樣的行爲,但我分類了UITableViewCell。嘗試覆蓋自定義單元格上的setSelected方法。

+0

我應該只派生一個UITableViewCell,或者我應該繼承我的UITableView? – Kuba 2012-08-15 14:48:25

+0

UITableViewCell的子類。在-drawRect方法中,您可以設置視圖並在-setSelected中,您可以更改處於選定狀態和未選中狀態的單元格的行爲和界面。 http://zcentric.com/2008/08/05/custom-uitableviewcell/ – btype 2012-08-16 06:00:22

+0

它解決了這個問題嗎? – btype 2012-08-16 17:49:39

-1

使用tableView.separatorColor(和tableView.separatorStyle)設置對比的分隔符顏色。如果您在單元格內繪製自己的分隔符,請設置separatorStyle = UITableViewCellSeparatorStyleNone。此外,將selectionStyle設置爲UITableViewCellSelectionStyleNone可能會幫助你

+0

我不想簡單地改變顏色或更改標準樣式。正如我所說 - 我想創建我自己的分隔符和選擇樣式。 – Kuba 2012-08-15 14:46:55

+0

如果你正在繪製你自己的分隔符,你應該考慮使用separatorStyle = UITableViewCellSeparatorStyleNone。此外,將selectionStyle設置爲UITableViewCellSelectionStyleNone可能會幫助您 – CSmith 2012-08-15 15:30:59

相關問題