2014-09-30 83 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; 
     } 
    }