簡體   English   中英

設置添加到表格視圖中單元格的圖像的大小

[英]setting the size of the image added to cell in tableview

我正在從plist將圖像添加到單元格視圖中。 另外,我也有使用URL保存到plist的圖像的相同問題。 他們是不同的大小。 我想將它們調整為特定大小,以便它們看起來都一樣。 我嘗試使用:

cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;

這沒用。 我嘗試了其他方法,但沒有成功。 我查了一堆東西,找不到任何有用的東西。 這是我的行方法單元格:

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

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"city"];
        cell.detailTextLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"state"];
        cell.imageView.image = [UIImage imageNamed:[[self.searchResults objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
        cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
        cell.imageView.clipsToBounds = YES;
    } else {
        cell.textLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"city"];
        cell.detailTextLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"state"];
        // Construct the expected URL for the image.
        NSURL *imageFileURL = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
        imageFileURL = [imageFileURL URLByAppendingPathComponent:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"] isDirectory:NO];
        // Attempt to load the image at the above URL.
        cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:[imageFileURL path]];
        if (!cell.imageView.image)
            cell.imageView.image = [UIImage imageNamed:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
        cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
        cell.imageView.clipsToBounds = YES;
    }

    return cell;
}

對於我的一生,我無法弄清楚。 我知道這可能很容易解決,但我別無選擇。

編輯:

如建議的那樣,我創建了一個自定義單元,在其中聲明了layoutSubviews,然后按如下所示對單元進行了子類化:

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

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"city"];
        cell.detailTextLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"state"];
        cell.imageView.image = [UIImage imageNamed:[[self.searchResults objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
        //cell.textLabel.textColor = [UIColor colorWithRed:153.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:1];
    } else {

        cell.textLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"city"];
        cell.detailTextLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"state"];
        // Construct the expected URL for the image.
        NSURL *imageFileURL = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
        imageFileURL = [imageFileURL URLByAppendingPathComponent:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"] isDirectory:NO];
        // Attempt to load the image at the above URL.
        cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:[imageFileURL path]];
        if (!cell.imageView.image)
            cell.imageView.image = [UIImage imageNamed:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
    }

    return cell;
}

再次,它不起作用。 這是屏幕截圖:

在此處輸入圖片說明

您將繼承UITableViewCell。 但您不必按照Dante的建議創建一個全新的視圖-layoutSubviews ,而是覆蓋-layoutSubviews

@interface VideoItemCell : UITableViewCell
@end


@implementation VideoItemCell
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, self.imageView.frame.origin.y, 100, 100);
}

@end

在tableview控制器中

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

    //…
    return cell;
}

默認的TableViewCell根據圖像大小調整imageview的大小。 為了解決這個問題,您可以使用具有自定義Image View的自定義單元格。

有關如何創建自定義單元的更多信息

如何從Xib文件加載自定義UITableViewCells?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM