2011-07-22 28 views
6

我做了一個UITableView具有不同高度的單元格。 在每個單元格上,我都附加了一個accessoryView(UIImageView)。UITableViewCell accessoryType位置更改與單元格高度

根據單元的高度,accessoryType位置不同(請參閱附加圖像)。 我該如何改正它?

在任何表格視圖中似乎都是正常的。沒有找到答案。

感謝您的幫助。 http://i.stack.imgur.com/kKOn0.png

UPDATE:我的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

你怎麼想這個形象是什麼樣子? – Legolas

+0

附件通常在單元中垂直居中,並且必須在相同的X軸上完全對齊。 –

+0

請提供您的'cellForRowAtIndexPath'。 – Legolas

回答

23

我覺得accessoryView的定位,使得從右側,頂部和底部邊緣的距離在某種程度上是相關的,所以你必須重新定位。

也許最好的辦法是子類的UITableViewCell,並重寫layoutSubviews方法:

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

這是正確的方法! 非常感謝@myszon。 –

+0

嗨, 我也有同樣的問題,但它說Expression不可轉讓。請你幫忙嗎? –

+8

你不能直接在這個上下文中對x和y應用一個值,而應該這樣做:self.accessoryView.frame = CGRectMake(self.accessoryView.frame.origin.x,10.0,self.accessoryView.frame.size .width,self.accessoryView.frame.size.height); –

相關問題