2014-01-24 79 views
2

我正在開發一個應用程序,在iOS7。我有一個TableViewController,它的風格是分組的。 我成功實施了this解決方案,可以根據需要獲取到TableView的曲線。iOS7奇怪的UITableViewCell選擇

編輯: -我沒有使用任何自定義類的單元格。我已經添加TableView只是從筆尖,也使用默認的單元格。實施

代碼是如下: -

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([cell respondsToSelector:@selector(tintColor)]) { 
     if (tableView == self.tblviwSample) { 
      CGFloat cornerRadius = 5.f; 
      cell.backgroundColor = UIColor.clearColor; 
      CAShapeLayer *layer = [[CAShapeLayer alloc] init]; 
      CGMutablePathRef pathRef = CGPathCreateMutable(); 
      CGRect bounds = CGRectInset(cell.bounds, 10, 0); 
      BOOL addLine = NO; 
      if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { 
       CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius); 
      } else if (indexPath.row == 0) { 
       CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds)); 
       CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius); 
       CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); 
       CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds)); 
       addLine = YES; 
      } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { 
       CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds)); 
       CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius); 
       CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); 
       CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds)); 
      } else { 
       CGPathAddRect(pathRef, nil, bounds); 
       addLine = YES; 
      } 
      layer.path = pathRef; 
      CFRelease(pathRef); 
      layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor; 

      if (addLine == YES) { 
       CALayer *lineLayer = [[CALayer alloc] init]; 
       CGFloat lineHeight = (1.f/[UIScreen mainScreen].scale); 
       lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight); 
       lineLayer.backgroundColor = tableView.separatorColor.CGColor; 
       [layer addSublayer:lineLayer]; 
      } 
      UIView *testView = [[UIView alloc] initWithFrame:bounds]; 
      [testView.layer insertSublayer:layer atIndex:0]; 
      testView.backgroundColor = UIColor.clearColor; 
      cell.backgroundView = testView; 
     } 
    } 
} 

這段代碼的作用 - See this - before selection

但得到一個問題,當我選擇一個單元格,它看起來怪異像this

我想該節以適當的方式。任何想法將不勝感激。

+1

我在這個問題只是偶然今天也。我剛剛將單元格的「selectionStyle」更改爲「UITableViewCellSelectionStyleNone」,直到找到可靠的解決方案。單元格收到了選擇,但沒有突出顯示。 – Isuru

回答

1

我會讓桌面視圖更薄(從兩側減少寬度),然後實現你的外觀,以便該行不會在表視圖的邊緣之前結束,但一直延伸到tableview的一側。

換句話說 - 減少表格視圖的寬度以匹配當前行寬,並保持行的出價,因爲它現在(它會像tableview一樣寬)。您的選擇現在也不會太寬。

或者,選擇後,您可以修改您的表格視圖單元格,以便模擬一些自定義選擇。這可能是通過改變細胞顏色直到用動畫翻轉它。

希望有所幫助。

+0

請用清楚的答案來解答。如果你能詳細說明答案,這將有所幫助 – iOSdev

+0

我不明白有什麼需要詳細說明的。在提供的圖像中,您將表視圖縮小爲與行一樣寬。然後選擇將與行一樣寬。設計並不要求tableview與整個屏幕一樣寬。 – avuthless

0

UITableViewCells選擇,所有子視圖得到[UIColor clearColor]默認情況下backgroundcolor,細胞selectedBackgroundView是設置事情是如何看的一個。

在您的codesample中,您正在操作bakcgroundview以獲得圓角,但只要選中一個單元格,此視圖就會變爲透明。我會建議覆蓋setSelected,並在這裏操作單元格子視圖。

UITableViewCellsselectedBackgroundView文檔:

默認值是在普通樣式的表 (UITableViewStylePlain)和非無爲部分基團表 UITableViewStyleGrouped)細胞爲零。僅當選中單元格時,UITableViewCell纔會將此 屬性的值作爲子視圖添加。如果它不是零,或者在所有其他視圖之後,它將 選定的背景視圖作爲子視圖直接添加到背景 視圖(backgroundView)之上。 調用setSelected:animated:使選定的背景視圖到 使用alpha淡入淡出和淡出。

1

今天也有這個問題,並且這裏的修復:

添加以下代碼willDisplayCell的末尾:

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
maskLayer.frame = cell.bounds; 
maskLayer.path = pathRef; 
cell.selectedBackgroundView.layer.mask = maskLayer; 

CFRelease(pathRef); 
+0

這適用於所選單元格問題。 – nath