我制作了一个UITableView,其中的单元格有不同的高度。在每个单元格上,我附加了一个accessoryView (UIImageView)。
根据单元格的高度,accessoryType的位置是不同的(参见附图)。我该如何改正呢?
这似乎在任何表视图中都是正常的。没有找到答案
谢谢你的帮助。https://i.stack.imgur.com/kKOn0.png
更新: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:@"%@nn%@", [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;
}
我认为accessoryView的定位使从右,上,下边缘的距离在某种程度上是相关的,所以你必须重新定位它。
可能最好的方法是继承UITableViewCell,并覆盖layoutSubviews方法:
- (void)layoutSubviews {
[super layoutSubviews];
CGRect accessoryFrame = self.accessoryView.frame;
accessoryFrame.origin.x = <desired position here>;
self.accessoryView.frame = accessoryFrame;
}