UIImageView 没有出现或大小不规则

Posted

技术标签:

【中文标题】UIImageView 没有出现或大小不规则【英文标题】:UIImageView not appearing or of irregular sizes 【发布时间】:2015-04-23 16:24:11 【问题描述】:

当我加载我的 tableView 时,图像会随着我通过 uisegmentedcontrol 过滤而改变大小。

这是我的 tableView 的代码(我有 UIImageView 的宽度和高度的固定约束,但不知何故它们没有遵守):

#import "ScheduleTableViewController.h"
#import "TFHpple.h"
#import "Game.h"
#import "ScheduleCellTableViewCell.h"
#import "GameModel.h"
#define team @"Muskegon"

@interface ScheduleTableViewController ()

    GameModel *_homeModel;


@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@property (nonatomic, strong) NSMutableArray *omahaHomeGames;
@property (nonatomic, strong) NSMutableArray *omahaAwayGames;
@property (nonatomic, strong) NSMutableArray *omahaAllGames;
@property (nonatomic, strong) NSArray *objects;
@property UIActivityIndicatorView *spinner;

@end

@implementation ScheduleTableViewController

-(void)itemsDownloaded:(NSArray *)items

    // This delegate method will get called when the items are finished downloading



    // Filter through all the games for the games that contain Omaha as a home     team and add them to the property omahaHomeGames
    for (Game *game in items) 
        if ([game.homeTeam containsString:team]) 
            [_omahaHomeGames addObject:game];
        
    
    // Filter through all the games for the games that contain Omaha as an away team and add them to the property omahaAwayGames
    for (Game *game in items) 
        if ([game.awayTeam containsString:team]) 
        [_omahaAwayGames addObject:game];
        
    
    // Filter through all the games for the games that contain Omaha as a home or away team and add them to the property omahaAllGames
    for (Game *game in items) 
        if ([game.homeTeam containsString:team] || [game.awayTeam containsString:team]) 
            [_omahaAllGames addObject:game];
        
    

    [_spinner stopAnimating];

    // Reload the table view
    [self.tableView reloadData];


- (instancetype) initWithStyle:(UITableViewStyle)style

    self = [super initWithStyle:style];

    if (self) 

        // Create new HomeModel object and assign it to _homeModel variable
        _homeModel = [[GameModel alloc] init];

        // Set this view controller object as the delegate for the home model object
        _homeModel.delegate = self;

        // Call the download items method of the home model object
        [_homeModel downloadItems];

        // add a segmented control to filter by Home/Away
        NSArray *divisions = [[NSArray alloc] initWithObjects:@"All", @"Home", @"Away", nil];
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:divisions];
        segmentedControl.frame = CGRectMake(0, 0, 120, 30);

        segmentedControl.tintColor = [UIColor orangeColor];

        [segmentedControl addTarget:self action:@selector(filterByHomeAway:) forControlEvents:UIControlEventValueChanged];
        segmentedControl.selectedSegmentIndex = 0;
        self.navigationItem.titleView = segmentedControl;
        self.segmentedControl = segmentedControl;

        // alloc and init properties
        self.omahaAllGames = [[NSMutableArray alloc] init];
        self.omahaHomeGames = [[NSMutableArray alloc] init];
        self.omahaAwayGames = [[NSMutableArray alloc] init];
    
    return self;


- (IBAction)filterByHomeAway:(id)sender

    NSLog(@"Filter: %ld", (long)self.segmentedControl.selectedSegmentIndex);
    [self.tableView reloadData];


- (void)viewDidLoad 
    [super viewDidLoad];

    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
                                        initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;

    spinner.center = CGPointMake(screenWidth/2.0, screenHeight/5.0);
    spinner.hidesWhenStopped = YES;
    [self.view addSubview:spinner];
    [spinner startAnimating];
    self.spinner = spinner;


    // Load the Cell NIB file
    UINib *nib = [UINib nibWithNibName:@"ScheduleCellTableViewCell" bundle:nil];

    // Register this NIB, which contains the cell
    [self.tableView registerNib:nib forCellReuseIdentifier:@"ScheduleCell"];


    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

   // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;


- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    // Return the number of sections.
    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section 
    // Return the number of rows in the section.
    if (self.segmentedControl.selectedSegmentIndex == 0) 
        return (self.omahaAllGames.count);
    
    if (self.segmentedControl.selectedSegmentIndex == 1) 
        return (self.omahaHomeGames.count);
    
    if (self.segmentedControl.selectedSegmentIndex == 2) 
        return self.omahaAwayGames.count;
    
    return 0;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    // Get a new or recycled cell
    ScheduleCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ScheduleCell" forIndexPath:indexPath];

    if (self.segmentedControl.selectedSegmentIndex == 0) 
        _objects = _omahaAllGames;
    
    if (self.segmentedControl.selectedSegmentIndex == 1) 
        _objects = _omahaHomeGames;
    
    if (self.segmentedControl.selectedSegmentIndex == 2) 
        _objects = _omahaAwayGames;
    

    // Configure the cell
    Game *thisGame = [_objects objectAtIndex:indexPath.row];

    if (self.segmentedControl.selectedSegmentIndex == 0) 
        cell.versusLabel.text = [NSString stringWithFormat:@"%@ vs. %@", thisGame.homeTeam, thisGame.awayTeam];

    if (self.segmentedControl.selectedSegmentIndex == 1) 
        cell.versusLabel.text = [NSString stringWithFormat:@"%@ vs. %@", team, thisGame.awayTeam];
    
    if (self.segmentedControl.selectedSegmentIndex == 2) 
        cell.versusLabel.text = [NSString stringWithFormat:@"%@ vs. %@", team, thisGame.homeTeam];
    

    // Trim the white space from before and after the date/time
    NSString *day = [thisGame.dayString substringFromIndex: 8];
    NSString *dayModified = [day substringWithRange: NSMakeRange(0, 11)];
    NSString *time = [thisGame.timeString substringFromIndex:9];
    NSString *timeModified = [time substringWithRange: NSMakeRange(0, 8)];
    NSString *dateAndTime = [NSString stringWithFormat:@"%@  %@", dayModified, timeModified];


    cell.dateLabell.text = dateAndTime;
    cell.locationLabel.text = thisGame.arena;


    // Assign the image of the away team if home, and the home team if away, and the team that isn't Omaha if all
    if (self.segmentedControl.selectedSegmentIndex == 0) 
        [thisGame assignAllTeamImage];
        cell.imageView.image = thisGame.image;
    
    if (self.segmentedControl.selectedSegmentIndex == 1) 
        [thisGame assignHomeTeamImage];
        cell.imageView.image = thisGame.image;
    
    if (self.segmentedControl.selectedSegmentIndex == 2) 
        [thisGame assignAwayTeamImage];
        cell.imageView.image = thisGame.image;
    

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    return cell;


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    return 70;


@end

如何使它们保持一致的大小?我似乎无法让它工作。谢谢。

【问题讨论】:

【参考方案1】:

不要在单元格的默认 imageView 中显示图像,而是尝试创建一个新的 imageView 并将其添加到单元格中。

    UIImageView *imageViewIcon = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 50, 50)];
        [cell addSubview:imageViewIcon];

if (self.segmentedControl.selectedSegmentIndex == 0) 
        [thisGame assignAllTeamImage];
        //cell.imageView.image = thisGame.image;
        imageViewIcon.image = thisGame.image;
    
    if (self.segmentedControl.selectedSegmentIndex == 1) 
        [thisGame assignHomeTeamImage];
        //cell.imageView.image = thisGame.image;
        imageViewIcon.image = thisGame.image;
    
    if (self.segmentedControl.selectedSegmentIndex == 2) 
        [thisGame assignAwayTeamImage];
        //cell.imageView.image = thisGame.image;
        imageViewIcon.image = thisGame.image;
    

在上面的代码中,根据您的要求更改 imageViewIcon 的框架。

而且由于您使用的是 dequeueReusableCellWithIdentifier,请不要忘记给 imageViewIcon 一个标签并在开始时将其删除。

希望这对您有所帮助。 :)

【讨论】:

以上是关于UIImageView 没有出现或大小不规则的主要内容,如果未能解决你的问题,请参考以下文章

使 UIImageView 及其 UIImage 按比例缩放而无需额外的填充

UIImageView 没有动画

在 UIImageview 上覆盖 UIImageview 保存

UIImageView 上的按钮不可点击

Objective-C:捕获后使用原始图像大小

Swift - 在 3 个方向(顶部、右侧或底部)随机旋转 UIImageView