【发布时间】:2012-07-28 00:36:50
【问题描述】:
我正在尝试制作一个基本的照片网格,即一个由小“缩略图”图像组成的“网格”,当点击它时会转到图像的大版本。我正在使用 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]。 -
就我个人而言,我使用 UIScrollView 作为我的画廊网格,然后在
viewWillLayoutSubviews上我只是相应地调整框架。但这是一个更实质性的重写。 -
是的,如果您以横向模式开始,它只有在您以纵向开始然后旋转它时才能正常工作。问题是,如果您在加载
[self.tableview reloadData];之后调用它,则 cellForRowAtIndexPath 中的 if 语句会插入并不会调用函数的其余部分,除非您将 self.photoIndex 设置回 0,这会弄乱活动指示器。 -
好的,我看到你尝试了
reloadData,但没有注意到你有所有的条件逻辑。是的,您只需要使用不同的解决方案来了解图像是否已下载。依赖 self.photoIndex 不是正确的解决方案。无论表格视图单元是创建还是成功出列,创建新的 UIButtons 也是有问题的。正如从viewWillAppear调用viewDidLoad一样(即使您是根据网络状态的变化有条件地这样做)。最重要的是,我在这里看到了很多东西。 -
我们应该把它移到聊天中(而不是在 cmets 中做详细的事情)吗? chat.stackoverflow.com/rooms/14554/…
标签: ios cocoa-touch ipad rotation ipod