我正在嘗試製作一個基本的照片網格,即一個小的「縮略圖」圖像的「網格」,點擊時會轉到大圖像。我正在使用UITableView來完成此操作。我的問題是,當我嘗試添加旋轉支持時,我不能讓表格重新繪製一排額外的圖像。現有的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);
}
任何幫助的感謝吧!
碎紙機
我還沒有經歷代碼,但我正確的假設它在橫向模式下工作,如果你在橫向模式下啓動,它只是方向的變化是有問題的?如果是這樣,只需在'willAnimateRotationToInterfaceOrientation'中調用'[self.tableview reloadData]'。 – Rob 2012-07-28 01:34:19
就我個人而言,我使用UIScrollView作爲我的畫廊網格,然後在'viewWillLayoutSubviews'上,我只是相應地調整框架。但這是一個更重大的重寫。 – Rob 2012-07-28 01:35:59
是的,如果你在lanscape模式下開始,它只有在以肖像開始然後旋轉時才能正常工作。問題是如果你調用'[self.tableview reloadData]; '在載入cellForRowAtIndexPath中的if語句後,doesent調用函數的其餘部分,除非您將self.photoIndex設置回0,從而混淆了活動指示符。 – Shredder2794 2012-07-28 03:20:19