2014-09-30 13 views
11

Chcę utworzyć znaczek, który jest wstawiany do komórek accesoryView. Wiem, że w przeszłości było to bardzo często zadawane, ale te podklasy Github wydają się być przestarzałe i nie mogą być przeniesione do szybkiego projektu. Jaki jest najłatwiejszy sposób na zrobienie tego w IOS w szybkim projekcie?Widok znaczka w widoku UITableViewCell

coś takiego:

enter image description here

+0

którym 'te GitHub podklasy' Czy na myśli? – AlBlue

+1

https://github.com/tmdvs/TDBadgedCell – gamerChristian

+0

Myślę, że będziesz musiał dostosować UITableViewCell. Nie jestem pewien, jak to zrobić z Swift, ale można znaleźć mnóstwo tutoriala w Internecie dla Objective-C. Oto jeden, http://www.appcoda.com/customize-table-view-cells-for-uitableview/ –

Odpowiedz

3

użyłem tego kodu w 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

To jest całkiem fajne. Próbowałem to wdrożyć w projekcie Swift 2.0. Ale 'WidthForView' jest niedostępny. Czy możesz podać rozwiązanie do obliczenia szerokości ciągu –

+0

func widthForView (_ tekst: String, czcionka: UIFont, wysokość: CGFloat) -> CGFloat { Niech etykieta: UILabel = UILabel (frame: CGRect (x: 0, y 0, szerokość CGFloat.greatestFiniteMagnitude, wysokość: wysokość)) label.numberOfLines = 1 label.lineBreakMode = NSLineBreakMode.byWordWrapping label.font = czcionki label.text = tekst label.textAlignment = .center etykiet. sizeToFit() return label.frame.width } –

25

Poniższy kod testuje w iOS7 oraz systemów iOS 8 i działa w następujący sposób:

  1. Jeśli liczba> zero, wyświetli liczbę w białym tekście z ponownym wyświetleniem d znaczek wokół niego.
  2. Jeśli count = zero, wyświetli prosty wskaźnik ujawnienia.



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; 
     } 
    } 
Powiązane problemy