2011-07-22 17 views
6

Zrobiłem UITableView z komórkami, które mają różną wysokość. Na każdej komórce zainstalowałem narzędzie accessoryView (UIImageView).UITableViewCell accessoryRozmieszczenie pozycji z wysokością komórki

W zależności od wysokości celi pozycja accessoryType jest inna (patrz załączony obraz). Jak mogę to poprawić?

Wygląda na normalny w każdym widoku tabeli. Nie znaleziono odpowiedzi.

Dzięki za pomoc. http://i.stack.imgur.com/kKOn0.png

UPDATE: My cellForRowAtIndexPath:

NSDictionary *currentTime = [listOfTimes objectAtIndex:indexPath.row]; 


UITableViewCell *cell; 
if([[currentTime objectForKey:@"freq"] intValue] > 0) cell = [self getCellFreqContentView:@"CellFreq"]; 
else cell = [self getCellContentView:@"Cell"]; 

[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4]; 
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter setDateStyle:NSDateFormatterNoStyle];  
[dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 

UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1]; 
UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2]; 

if([[currentTime objectForKey:@"freq"] intValue] > 0) { 

    lblTemp1.text = [NSString stringWithFormat:@"%@\n\n%@", [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceReferenceDate:[[currentTime objectForKey:@"arrival_time_seconds"] doubleValue]]],[dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceReferenceDate:[[currentTime objectForKey:@"departure_time_seconds"] doubleValue]]]]; 

    UILabel *lblTemp3 = (UILabel *)[cell viewWithTag:3]; 
    UILabel *lblTemp4 = (UILabel *)[cell viewWithTag:4];   
    lblTemp3.text = @"↓"; 
    lblTemp4.text = [NSString stringWithFormat:NSLocalizedString(@"A pass every %i minutes", nil), [[currentTime objectForKey:@"freq"] intValue]/60]; 
} else { 
    lblTemp1.text = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceReferenceDate:[[currentTime objectForKey:@"departure_time_seconds"] doubleValue]]]; 
} 

lblTemp2.text = [currentTime objectForKey:@"trip_headsign"]; 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

UIView *vv = [[UIView alloc] initWithFrame:cell.frame]; 
if(indexPath.row == [ScheduleManager getRowForTime:listOfTimes] && isCurrentPeriod == TRUE) { 
    vv.backgroundColor = [BKColor colorWithHexString:@"54B8BB"]; 
} 

// SPECIAL CASES 
if([[currentTime valueForKey:@"runoff"] count] > 0 && useExceptions == TRUE) { 
    // ACCESSORY 
    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"times-special.png"]]; 
} 

cell.backgroundView = vv; 

return cell; 
} 
+0

Jak chcesz to zdjęcie wyglądać? – Legolas

+0

Dodatek jest zazwyczaj wyśrodkowany pionowo w komórce i musi być wyrównany do prawej strony na tej samej wartości osi X. –

+0

Podaj swoją 'cellForRowAtIndexPath' proszę. – Legolas

Odpowiedz

23

myślę accessoryView jest umieszczony tak, że odległość od prawej, górnej i dolnej krawędzi jest skorelowana w jakiś sposób, więc trzeba go przesunąć.

Prawdopodobnie najlepszym sposobem jest do podklasy UITableViewCell i zastąpić metodę layoutSubviews:

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    CGRect accessoryFrame = self.accessoryView.frame; 
    accessoryFrame.origin.x = <desired position here>; 
    self.accessoryView.frame = accessoryFrame; 
} 
+0

To jest właściwa droga! Wielkie dzięki @myszon. –

+0

Witam, Mam również ten sam problem, ale jest napisane, że wyrażenia nie można przypisać.CAn, proszę o pomoc? –

+8

Nie można bezpośrednio zastosować wartości do x i y w tym kontekście, zamiast zrobić coś takiego: self.accessoryView.frame = CGRectMake (self.accessoryView.frame.origin.x, 10.0, self.accessoryView.frame.size .width, self.accessoryView.frame.size.height); –

Powiązane problemy