2013-10-04 15 views
9

Seit der Einführung von iOS7, abgerundete Ecken für die Zellen von UITableView mit gruppierten Stil wird von Apple (confirmed here) entfernt. Trotzdem bin ich mir sicher, dass es kluge Leute gibt, die dafür eine Umgehung geschaffen haben.Workaround für abgerundete Ecken von gruppierten UITableView, iOS7

Bitte, um mir und vielen anderen Kollegen iOS-Programmierer, Post Ihre Workarounds zu runden Ecken für die Zellen in einem UITableViewStyleGrupped in iOS7 in diesem Thread. Es wird sehr geschätzt werden!

+0

was Sie über die Antworten denken? –

+0

@RobertoFerraz Ich habe noch keine Zeit gehabt, deine Antwort auszuprobieren. Ich mag die Idee Ihrer Lösung, aber ich möchte sie testen, bevor ich sie auffrische oder als Lösung benenne. Entschuldigung, aber bitte verstehe meine guten Absichten. – OscarWyck

Antwort

5

Gerade folgende UITableView Delegatmethode in Ihrem Code hinzuzufügen. Es hat mir wirklich geholfen, das gleiche Problem wie Sie für die Tabellenansicht in iOS7. Versuchen Sie folgenden Code, möglicherweise ist es auch Ihr Problem gelöst:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([cell respondsToSelector:@selector(tintColor)]) { 
     if (tableView == tblProfileDetails) { // self.tableview 
      CGFloat cornerRadius = 5.f; 
      cell.backgroundColor = UIColor.clearColor; 
      CAShapeLayer *layer = [[CAShapeLayer alloc] init]; 
      CGMutablePathRef pathRef = CGPathCreateMutable(); 
      CGRect bounds = CGRectInset(cell.bounds, 5, 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)+5, bounds.size.height-lineHeight, bounds.size.width-5, 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; 
     } 
    } 
} 
+34

Wenn Sie [Code aus der Antwort eines anderen Benutzers] (http://stackoverflow.com/a/18833311/41116) kopieren und in eine andere Frage einfügen möchten, müssen Sie sie zumindest zuordnen. – Abizern

+0

Es half mir, mein Problem in meiner App zu lösen, nur aus diesem Grund füge ich diesen Code hier ein.Und es ist wirklich ein hilfreicher Thread. –

+0

Dieser Code hat funktioniert. Ich habe auch hinzugefügt: self.tableView.separatorColor = [UIColor clearColor]; was notwendig war, damit es gut aussieht. – OscarWyck

-4

können Sie UIImageView verwenden und den Eckenradius eingestellt wie folgt:

CALayer * l = [self.cellBackgroundImage layer]; 
[l setMasksToBounds:YES]; 
[l setCornerRadius:5.0]; 

diese nutzen zu können, sollten Quart Kern importieren:

#import <QuartzCore/QuartzCore.h> 
+1

Funktioniert nicht für gruppierte Zellen. – Matt

11

Betrachtet man mehrere Gruppen in der Tabelle haben kann, einige Zellen runden sich nur in den oberen Ecken ab, andere nur unten, andere sind nicht abgerundet. Also habe ich hier sehr schnell eine Unterklasse (das kann wirklich gekürzt werden) von UITableViewCell mit den folgenden Methoden:

Eine Eigenschaft, die Sie etwas nennen Sie wollen, rief ich oben und unten:

@property(nonatomic,assign) BOOL top,down; 

Die Umsetzung:

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    if(self.top && self.down) 
    { 
     self.layer.cornerRadius = 10; 
     self.layer.masksToBounds = YES; 
     return; 
    } 

    if (self.top) 
    { 
     CAShapeLayer *shape = [[CAShapeLayer alloc] init]; 
     shape.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)].CGPath; 
     self.layer.mask = shape; 
     self.layer.masksToBounds = YES; 
     return; 
    } 

    if (self.down) 
    { 
     CAShapeLayer *shape = [[CAShapeLayer alloc] init]; 
     shape.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)].CGPath; 
     self.layer.mask = shape; 
     self.layer.masksToBounds = YES; 
    } 


} 

Um einfach zu verwenden ist, wenn Ihre Zelle die erste einer Gruppe ist, Settop = true, wenn der letzte ist, AB = true, wenn die Zelle die einzige auf der Gruppe ist, nach oben und DOWN = wahr.

Wenn Sie mehr oder weniger gerundet möchten, ändern Sie einfach die Radios von 10 auf den anderen Wert.

Das ist es, Sie können es kürzer machen Vermeidung von Wiederholung von Code oder mit CAShapeLayer in allen Fällen, trotzdem hoffe ich, es hilft.

3

Hier ist eine leicht verbesserte Antwort auf die von Roberto Ferraz.

Er erkennt die obere und untere Zelle für Sie (vorausgesetzt, es gibt keine Lücke zwischen den Zellen).

Es nutzt auch hat Trick von remove borders from group die

@interface CustomTableViewCell : UITableViewCell 

Aufschalten Nachrichten in iOS7 funktioniert:

#define radius 10 

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    CGRect frame = self.frame; 
    CGFloat top = frame.origin.y; 
    CGFloat bottom = top + frame.size.height; 
    UIRectCorner corners = UIRectCornerAllCorners; 
    CAShapeLayer* mask = [CAShapeLayer layer]; 

    for (UIView* view in self.superview.subviews) { 
     if (view.frame.origin.y + view.frame.size.height == top) { 
      corners = corners & ~(UIRectCornerTopLeft|UIRectCornerTopRight); 
     } else if (view.frame.origin.y == bottom) { 
      corners = corners & ~(UIRectCornerBottomLeft|UIRectCornerBottomRight); 
     } 
    } 

    mask.path = [UIBezierPath bezierPathWithRoundedRect: 
       CGRectMake(0, 0, frame.size.width, frame.size.height) 
             byRoundingCorners:corners 
          cornerRadii:CGSizeMake(radius, radius)].CGPath; 

    self.layer.mask = mask; 
    self.layer.masksToBounds = YES; 
} 

/* 
iOS 7 workaround to get rid of top and bottom separators 
*/ 
- (void) addSubview:(UIView*)view { 
    if (view.frame.origin.x > 0 || view.frame.size.width < self.frame.size.width) { 
     [super addSubview:view]; 
    } 
}