2015-04-23 2 views
-1

Когда я загружаю свой tableView, изображения меняют размеры, когда я фильтрую через uisegmentedcontrol. See here. See here.UIImageView не отображается или имеет нерегулярные размеры

Вот код для моего 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 

Как сделать их оставаться постоянным? Я просто не могу заставить его работать. Спасибо.

ответ

0

Вместо того, чтобы отображать изображение в изображении по умолчанию для изображения, попробуйте создать новое изображение и добавить его в ячейку.

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; 
    } 

В вышеуказанном коде смените рамку изображенияViewIcon согласно вашему требованию.

И поскольку вы используете dequeueReusableCellWithIdentifier, не забудьте дать тегу imageViewIcon и удалить его в начале.

Надеюсь, это вам поможет. :)

Смежные вопросы