2012-02-25 27 views
5

khi thêm một hình ảnh để ô trong bảng, như mặc định nó đi sang trái:Liên kết hình ảnh UITableViewCell ở bên phải, có thể?

cell.imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:indexPath.row]]; 

làm thế nào tôi có thể thay đổi nó rằng mỗi hình ảnh trong UITableViewCell sẽ đi automaticlly sang bên phải và textLabel sẽ được 10px bên trái của hình ảnh.

Cảm ơn rất nhiều!

+0

Tôi sợ bạn không thể với standar 'UITableViewCell' bạn phải sử dụng ô của riêng bạn rồi tùy chỉnh nó. – HelmiB

Trả lời

10

Một cách khác là tạo ra một tế bào tùy chỉnh và ghi đè layoutSubviews phương pháp.

@interface CustomCell : UITableViewCell 

@end 

@implementation CustomCell 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    // grab bound for contentView 
    CGRect contentViewBound = self.contentView.bounds; 
    // grab the frame for the imageView 
    CGRect imageViewFrame = self.imageView.frame; 
    // change x position 
    imageViewFrame.origin.x = contentViewBound.size.width - imageViewFrame.size.width; 
    // assign the new frame 
    self.imageView.frame = imageViewFrame; 
} 

@end 

Rembember rằng trong cellForRowAtIndexPath bạn cần để tạo ra và tái sử dụng CustomCell và không UITableViewCell.

Hy vọng điều đó sẽ hữu ích.

Sửa

#import "CustomCell.h" 

// other code here... 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCell"; 
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    return cell; 
} 
+0

Cảm ơn bạn đã giải pháp. PS: Không phải imageViewFrame.position.x nhưng imageViewFrame.origin.x – uerceg

+0

Giải pháp tuyệt vời! Tôi đã sử dụng mã của bạn để căn giữa hình ảnh – louissmr

+0

Sử dụng một phiên bản nhanh chóng của điều này và nó hoạt động tốt. – LondonGuy

1

thử điều này:

cell.imageView.frame = CGRectMake(cell.frame.size.width - cell.imageView.frame.size.width, cell.imageView.frame.origin.y, cell.imageView.frame.size.width, cell.imageView.frame.size.height); 
[cell.yourTexLabel sizeToFit]; 
cell.yourTexLabel.frame = CGRectMake(cell.imageView.origin.x - cell.yourTexLabel.frame.size.width - 10, cell.yourTexLabel.frame.origin.y, cell.yourTexLabel.frame.size.width, cell.yourTexLabel.frame.size.height); 
+0

không có dude ... nó không hoạt động. Ngoài ra, 'yourTexLabel' là 'cell.textLabel, nhưng anh ấy không thể nhận ra nó vì một lý do nào đó .. –

+0

ồ, anh bạn ... tôi đoán bạn đang sử dụng một lớp con của riêng bạn ... bạn có thể' t làm quá nhiều với các tế bào tiêu chuẩn ... – meronix

0

Một giải pháp là sử dụng một tùy chỉnh UITableViewCell. Các bước là:

  1. Tạo lớp khách quan-C mới là phân lớp UITableViewCell, ví dụ: LabeledImageTableViewCell. Khai báo ivarsthuộc tính cho số UILabelUIImageView.
  2. Trong Trình tạo giao diện, đặt nội dung của UITableView đến Mẫu thử động. Kéo một số UIImageViewUILabel vào ô xem bảng và định vị chúng. Đặt lớp của ô thành LabeledImageTableViewCell. Kết nối các cửa hàng của ô với các đối tượng UILabel & UIImageView của LabeledImageTableViewCell.
  3. Trong các đại biểu cho UITableView (thường là một UITableViewController, đôi khi một UIViewController) thực hiện nguồn dữ liệu phương pháp, ví dụ:

    //#pragma mark - Table view data source 
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
        return 1; 
    } 
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
        return (NSInteger)[rowData count]; 
    } 
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        static NSString *CellIdentifier = @"tvcLabeledImage"; 
        LabeledImageTableViewCell *cell = (LabeledImageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
        if (cell == nil) { 
         cell = [[LNCCorrelationInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
        } 
        cell.myImage = [UIImage imageNamed:@"imageForThisRow.png"]; 
        cell.myLabel = "imageForThisRow"; 
        return cell; 
    } 
    

Ngoài ra, hãy kiểm tra các đoạn video của Apple từ WWDC 2011, Thay đổi UITableView, Mẹo & Thủ thuậtGiới thiệu Bảng phân cảnh bộ dựng giao diện (Yêu cầu đăng nhập: https://developer.apple.com/videos/wwdc/2011/.)

5

Tìm giải pháp ở đây mã.

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]]; 
cell.accessoryView = imageView; 

Để bạn tham khảo.

UITableViewCell with image on the right?

+0

Cảm ơn Narasimha Nallamsetty ;) – Abo3atef

1

Tìm thấy một câu trả lời tốt hơn từ @TomSwift đây https://stackoverflow.com/a/31616694/1884707

cell.contentView.transform = CGAffineTransformMakeScale(-1,1); 
cell.imageView.transform = CGAffineTransformMakeScale(-1,1); 
cell.textLabel.transform = CGAffineTransformMakeScale(-1,1); 
cell.textLabel.textAlignment = NSTextAlignmentRight; 

Bằng cách áp dụng một biến đổi trên contentView bạn có thể đặt IMAGExem ở bên phải.

0

trong swift3 và swift4, chúng ta có thể sử dụng này:

cell.accessoryView = UIImageView (ảnh: UIImage (tên: "imageNmae"))

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