2011-07-22 32 views
6

Tôi đã tạo một UITableView với các ô có chiều cao khác nhau. Trên mỗi ô, tôi đã đính kèm một accessoryView (UIImageView).Thay đổi vị trí loại phụ kiện UITableViewCell với chiều cao ô

Tùy thuộc vào chiều cao của ô, vị trí accessoryType là khác nhau (xem hình ảnh đính kèm). Tôi làm cách nào để sửa?

Dường như bình thường trong bất kỳ chế độ xem bảng nào. Không tìm thấy câu trả lời nào.

Cảm ơn sự giúp đỡ của bạn. http://i.stack.imgur.com/kKOn0.png

UPDATE: cellForRowAtIndexPath của tôi:

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

Làm thế nào để bạn muốn hình ảnh này nhìn như thế nào? – Legolas

+0

Phụ kiện thường được căn giữa theo chiều dọc trong ô và phải được căn chỉnh đúng trên cùng một giá trị trục X. –

+0

Vui lòng cung cấp 'cellForRowAtIndexPath' của bạn. – Legolas

Trả lời

23

Tôi nghĩ rằng accessoryView được bố trí sao cho khoảng cách từ phải, trên và cạnh đáy là tương quan một cách nào đó, vì vậy bạn phải định vị lại nó.

Có lẽ cách tốt nhất là để phân lớp UITableViewCell, và ghi đè lên layoutSubviews phương pháp:

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

Đó là cách phù hợp! Cảm ơn rất nhiều @myszon. –

+0

Xin chào, Tôi cũng có cùng một vấn đề nhưng nó nói Biểu thức không thể gán được.Có bạn vui lòng trợ giúp? –

+8

Bạn không thể trực tiếp áp dụng giá trị cho x và y trong ngữ cảnh này, thay vào đó hãy thực hiện một cái gì đó như sau: self.accessoryView.frame = CGRectMake (self.accessoryView.frame.origin.x, 10.0, self.accessoryView.frame.size .width, self.accessoryView.frame.size.height); –

Các vấn đề liên quan