2014-09-30 81 views
11

我想創建一個插入到單元格accesoryView中的badgeView。我知道這在過去曾經有過很多問題,但是那些github子類似乎已經過時了,不能被移植到一個快速的項目中。在一個快速項目中,在IOS中最簡單的方法是什麼?UITableViewCell中的徽章視圖

事情是這樣的:

enter image description here

+0

其中 '那些GitHub的子類' 你指什麼? – AlBlue 2014-09-30 22:01:23

+1

https://github.com/tmdvs/TDBadgedCell – gamerChristian 2014-09-30 22:12:17

+0

我認爲你需要定製UITableViewCell。我不知道如何用Swift做到這一點,但是您可以在Internet上找到大量Objective-C的教程。這裏是一個,http://www.appcoda.com/customize-table-view-cells-for-uitableview/ – 2014-09-30 22:46:17

回答

3

我用這個代碼中的cellForRowAtIndexPath

var accesoryBadge = UILabel() 
    var string = "2" 
    accesoryBadge.text = string 
    accesoryBadge.textColor = UIColor.whiteColor() 
    accesoryBadge.font = UIFont(name: "Lato-Regular", size: 16) 
    accesoryBadge.textAlignment = NSTextAlignment.Center 
    accesoryBadge.layer.cornerRadius = 4 
    accesoryBadge.clipsToBounds = true 

    accesoryBadge.frame = CGRectMake(0, 0, WidthForView(string, font: UIFont(name: "Lato-Light", size: 16), height: 20)+20, 20) 
    cell.accessoryView = accesoryBadge 
+0

這是漂亮的廚師。我試圖在Swift 2.0項目中實現它。但是'WidthForView'不可用。你能提供一個解決方案來計算字符串的寬度: – 2015-10-03 09:37:12

+0

func widthForView(_ text:String,font:UIFont,height:CGFloat) - > CGFloat let label:UILabel = UILabel(frame:CGRect(x:0,y :0,寬度:CGFloat.greatestFiniteMagnitude,高度:高度)) label.numberOfLines = 1 label.lineBreakMode = NSLineBreakMode.byWordWrapping label.font =字體 label.text =文本 label.textAlignment = .center 標籤。 sizeToFit() return label.frame.width } – 2018-01-11 17:10:37

25

下面的代碼已經在iOS7和iOS8上一直在測試和工作原理如下:

  1. 如果count>零,它將顯示帶有re的白色文本的計數在它周圍的D徽章。
  2. 如果count =零,它將顯示一個簡單的披露指標。



enter image description here


- (void)updateTableViewCell:(UITableViewCell *)cell forCount:(NSUInteger)count 
    { 
     // Count > 0, show count 
     if (count > 0) { 

      // Create label 
      CGFloat fontSize = 14; 
      UILabel *label = [[UILabel alloc] init]; 
      label.font = [UIFont systemFontOfSize:fontSize]; 
      label.textAlignment = NSTextAlignmentCenter; 
      label.textColor = [UIColor whiteColor]; 
      label.backgroundColor = [UIColor redColor]; 

      // Add count to label and size to fit 
      label.text = [NSString stringWithFormat:@"%@", @(count)]; 
      [label sizeToFit]; 

      // Adjust frame to be square for single digits or elliptical for numbers > 9 
      CGRect frame = label.frame; 
      frame.size.height += (int)(0.4*fontSize); 
      frame.size.width = (count <= 9) ? frame.size.height : frame.size.width + (int)fontSize; 
      label.frame = frame; 

      // Set radius and clip to bounds 
      label.layer.cornerRadius = frame.size.height/2.0; 
      label.clipsToBounds = true; 

      // Show label in accessory view and remove disclosure 
      cell.accessoryView = label; 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } 

     // Count = 0, show disclosure 
     else { 
      cell.accessoryView = nil; 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 
    }