2013-10-07 90 views
3

我有一個表視圖,我想通過使用下面的代碼在其上添加圖像,如何防止單元格圖像切割表格單元格邊框?

第一通過重寫layoutSubviews

- (void)layoutSubviews{ 
    [super layoutSubviews]; 
self.imageView.frame = CGRectMake(15, 5, 50, 45); 
self.imageView.layer.cornerRadius = 5.0f; 
self.imageView.layer.masksToBounds = YES; 
self.imageView.contentMode = UIViewContentModeScaleAspectFill; 
} 

然後在-(MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

NSString *imageUrlString = [userInfoResponse[indexPath.row - beginIndex] objectForKey:@"image_url"]; 
     NSURL * imageURL = [NSURL URLWithString:imageUrlString]; 
     if ([[NSUserDefaults standardUserDefaults] objectForKey:imageUrlString] == nil) { 
      [[NSUserDefaults standardUserDefaults] setObject:[NSData dataWithContentsOfURL:imageURL] forKey:imageUrlString]; 
     } 
     UIImage * img = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:imageUrlString]]; 
     cell.imageView.image = img; 
} 

使用它並在此行添加

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
return 55.0; 
} 

它可以很好地處理這個小問題(見附圖)。我不希望細胞邊界線被圖像中斷。

​​

我試着打碼,但它不會幫助。任何人都知道答案?請幫忙。謝謝

注意:它適用於iOS 6,此屏幕截圖在iOS 7上。它可以是iOS 7的新設置UITableView

+2

看起來您的圖像比單元格高。嘗試增加表格行的高度。 – tarmes

+0

@tarmes,我爲您添加了我的行高值,以查看我的圖像比行高小 – caribbean

回答

11

轉到您的表視圖控制器,並把這些代碼到viewDidLoad中:

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) //should check version to prevent force closed 
{ 
    self.tableView.separatorInset = UIEdgeInsetsZero; 
} 
+0

This Works!謝謝.. – caribbean

0

您的表格視圖單元格太小。在您的控制器中實施此方法並調整單元的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 60.0; 
} 
1

如果你不想調整UITableViewCell的高度,然後裏面cellForRowAtIndexPath使用:

[cell clipsToBounds:YES]; 
0

In viewDidLoad put:

[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)]; 
+0

爲什麼?有一點解釋會提供更好的答案。 –

1

上面的答案都不能完全擴展屏幕整個邊界的邊界。實現此目的的唯一方法是將以下代碼粘貼到ViewController中:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { 
    [cell setSeparatorInset:UIEdgeInsetsZero]; 
    } 

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { 
    [cell setLayoutMargins:UIEdgeInsetsZero]; 
    } 
} 

-(void)viewDidLayoutSubviews { 
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { 
    [self.tableView setSeparatorInset:UIEdgeInsetsZero]; 
    } 

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { 
    [self.tableView setLayoutMargins:UIEdgeInsetsZero]; 
    } 
}