我正在尝试制作一个基本的照片网格,即小“缩略图”图像的“网格”,单击时会转到图像的大版本。我正在使用 UITableView 来完成此操作。我的问题是,当我尝试添加旋转支持时,我无法让表格用额外的图像行重新绘制网格。
这是我的一些代码,请随时提出任何问题,因为我对此一无所知> :(
- (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(self.interfaceOrientation==UIInterfaceOrientationPortrait || self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown){
NSLog(@"portrait");
for (int i=0; i < self.numberOfColumns; i++) {
if (self.photoIndex < [self.photosArray count] || self.shouldReload==TRUE) {
UIButton *imageButton = [[UIButton alloc]init];
UIActivityIndicatorView *loadingActivityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[imageButton addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchDown];
switch (self.buttonNumber) {
case 1:
self.xCord=35.0f;
self.buttonNumber++;
break;
case 2:
self.xCord=213.25f;
self.buttonNumber++;
break;
case 3:
self.xCord=391.5f;
self.buttonNumber++;
break;
case 4:
self.xCord=569.75f;
self.buttonNumber=1;
break;
}
imageButton.frame = CGRectMake(self.xCord, 35.0f, 163.25f, 163.25f);
imageButton.tag = self.photoIndex;
imageButton.enabled=FALSE;
imageButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
if(self.buttonsArray.count < self.photosArray.count)[self.buttonsArray addObject:imageButton];
if([self.isInternetAvailableClass isInternetAvailable]==YES)[self downloadImages:self.photoIndex];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
[cell addSubview:imageButton];
if(self.photoIndex < self.buttonsArray.count){
if(![[self.buttonsArray objectAtIndex:self.photoIndex] currentImage]){
loadingActivityIndicator.center = imageButton.center;
loadingActivityIndicator.hidesWhenStopped=TRUE;
}
}
if(self.activityIndicatorArray.count < self.photosArray.count){
[self.activityIndicatorArray addObject:loadingActivityIndicator];
if([self.isInternetAvailableClass isInternetAvailable]==YES){
[loadingActivityIndicator startAnimating];
}
}else{
if(self.photoIndex < self.buttonsArray.count){
[self.activityIndicatorArray replaceObjectAtIndex:self.photoIndex withObject:loadingActivityIndicator];
}
}
[cell addSubview:loadingActivityIndicator];
self.photoIndex++;
}
}
return cell;
}else{
NSLog(@"landscape called!");
for (int i=0; i < self.numberOfColumns; i++) {
if (self.photoIndex < [self.photosArray count] || self.shouldReload==TRUE ){
NSLog(@"inside landscape called!");
UIButton *imageButton = [[UIButton alloc]init];
UIActivityIndicatorView *loadingActivityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[imageButton addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchDown];
switch (self.buttonNumber) {
case 1:
self.xCord=37;
self.buttonNumber++;
break;
case 2:
self.xCord=230;
self.buttonNumber++;
break;
case 3:
self.xCord=423;
self.buttonNumber++;
break;
case 4:
self.xCord=616;
self.buttonNumber++;
break;
case 5:
self.xCord=809;
self.buttonNumber=1;
break;
}
imageButton.frame = CGRectMake(self.xCord, 35.0f, 163.25f, 163.25f);
imageButton.tag = self.photoIndex;
imageButton.enabled=FALSE;
imageButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
if(self.buttonsArray.count < self.photosArray.count){
[self.buttonsArray addObject:imageButton];
}
if([self.isInternetAvailableClass isInternetAvailable]==YES){
[self downloadImages:self.photoIndex];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
[cell addSubview:imageButton];
if(self.photoIndex < self.buttonsArray.count){
if(![[self.buttonsArray objectAtIndex:self.photoIndex] currentImage]){
loadingActivityIndicator.center = imageButton.center;
loadingActivityIndicator.hidesWhenStopped=TRUE;
}
}
if(self.activityIndicatorArray.count < self.photosArray.count){
[self.activityIndicatorArray addObject:loadingActivityIndicator];
if([self.isInternetAvailableClass isInternetAvailable]==YES){
[loadingActivityIndicator startAnimating];
}
}else{
if(self.photoIndex < self.activityIndicatorArray.count){
[self.activityIndicatorArray replaceObjectAtIndex:self.photoIndex withObject:loadingActivityIndicator];
}
}
[cell addSubview:loadingActivityIndicator];
self.photoIndex++;
}
}
return cell;
}
if (self.shouldReload==TRUE)self.shouldReload=FALSE;
}
#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 205.0f;
}
#pragma mark - View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.photosTableView = [[UITableView alloc]init];
self.photosTableView.delegate=self;
self.photosTableView.dataSource=self;
self.photosTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.photosTableView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.photosTableView];
self.isInternetAvailableClass = [[IsInternetAvailable alloc]init];
self.buttonNumber=1;
self.xCord=0.0f;
self.downloadedImageNumber=0;
self.buttonsArray = [[NSMutableArray alloc]init];
self.dataDictionary = [[NSMutableDictionary alloc]init];
self.downloadedImagesArray = [[NSMutableArray alloc]init];
self.activityIndicatorArray = [[NSMutableArray alloc]init];
self.photosArray = [[NSMutableArray alloc]init];
self.largePhotosArray = [[NSMutableArray alloc]init];
self.imageOrderDictionary = [[NSMutableDictionary alloc]init];
self.refToDownloadedImage = [[UIImage alloc]init];
self.unformattedPhotosArray = [[NSMutableArray alloc]init];
self.appDelegate = (StereophotoAppDelegate *)[[UIApplication sharedApplication]delegate];
self.shouldReload=FALSE;
if(self.interfaceOrientation==UIInterfaceOrientationPortrait || self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown){
self.numberOfColumns=4;
}else{
self.numberOfColumns=5;
}
self.photoIndex=0;
self.errorImageArray = [[NSMutableArray alloc]init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewWillAppear:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(deviceDidRotate) name:UIDeviceOrientationDidChangeNotification object:nil];
[self loadImages];
}
-(void)viewWillAppear:(BOOL)animated{
self.appDelegate.isSlideshowRunning=NO;
self.appDelegate.PhotosPopoverSlideshowText = @"Start Slideshow";
if([self.isInternetAvailableClass isInternetAvailable]==YES){
if(self.isCommingFromNoNetworkConnectivity==YES){
[self viewDidLoad];
}
if(self.photosTableView.isHidden==YES)[self.photosTableView setHidden:NO];
self.isCommingFromNoNetworkConnectivity=NO;
}else{
if(self.photosTableView.isHidden==NO){
[self.photosTableView setHidden:YES];
self.isCommingFromNoNetworkConnectivity=YES;
}
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Oops!" message:@"No Internet connection was detected! Please connect to the Internet and try again!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[errorAlert show];
}
}
-(void)viewWillDisappear:(BOOL)animated{
for(NSURLConnection *connection in self.connectionsArray){
[connection cancel];
}
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return TRUE;
}
-(void)deviceDidRotate{
self.photosTableView.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
if(self.interfaceOrientation==UIInterfaceOrientationPortrait || self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown){
self.numberOfColumns=4;
}else{
self.numberOfColumns=5;
}
self.shouldReload=TRUE;
[self.photosTableView reloadData];
}
-(void)viewDidAppear:(BOOL)animated{
self.photosTableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}
谢谢你的帮助!
碎纸机