UIImageView没有出现或不规则的大小

时间:2015-04-23 16:24:12

标签: ios objective-c uitableview uiimageview uiimage

当我加载tableView时,图像会在我过滤uisegmentedcontrol时改变大小。 See here. See here.

这是我的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 个答案:

答案 0 :(得分:0)

尝试创建新的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,所以不要忘记给imageViewI一个标记并在开头删除它。

希望这会对你有所帮助。 :)