设置在tableview中添加到单元格的图像的大小

Adr*_*n P 1 objective-c uitableview ios ios7

我正在从plist中将图像添加到我的单元格视图中.此外,我对使用网址保存到plist的图像也有同样的问题.它们的大小不同.我想将它们调整到一定的大小,以便它们看起来都一样.我试过用:

cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)

那没起效.我尝试过其他一些东西并没有成功.我查了一堆东西,找不到任何有用的东西.这是我的行方法单元格:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

对于我的生活,我无法弄清楚这一点.我知道这可能是一个简单的修复,但我想不出别的.

编辑:

正如所建议的那样,我创建了一个自定义单元格,在那里声明了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;
}
Run Code Online (Sandbox Code Playgroud)

再一次,它没有用.这是屏幕截图:

在此输入图像描述

Vik*_*ica 7

你会继承UITableViewCell.但你不必创建一个由Dante提出的全新视图,但覆盖-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
Run Code Online (Sandbox Code Playgroud)

在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;
}
Run Code Online (Sandbox Code Playgroud)