【问题标题】:Xcode 5 iOS 7 TableView with DetailViewControllerXcode 5 iOS 7 TableView 与 DetailViewController
【发布时间】:2014-02-14 13:03:02
【问题描述】:

当我选择某个单元格时,它不显示 DetailViewController 这是来自 DetailViewController.h 的鳕鱼

#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
@property(nonatomic,strong) NSString *imageName;
@property(nonatomic,strong) IBOutlet UIImageView *imageView;
@end

来自 DetailViewController.m 的代码

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController
@synthesize imageName,imageView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    imageView.image=[UIImage imageNamed:imageName];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

和 ViewController.m` #import "ViewController.h" #import "DetailViewController.h"

@interface ViewController (){
    NSArray *soundtitles;
    NSArray *thumbs;
}

@end

@implementation ViewController
@synthesize tableView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
  //  self.title=@"Точки";
soundtitles = [[NSArray alloc]initWithObjects:@"novaposhta",@"ac-2",@"vugovskogo",@"naykova ",@"iskra",nil];
    thumbs=[[NSArray alloc]initWithObjects:@"thumbsnovaposhta.jpg",@"thumbsac-2.jpg",@"thumbvugovskogo.jpg",@"thumbnaykova.jpg",@"thumbsiskra.jpg", nil];
    tableView.backgroundColor=[UIColor clearColor];
    tableView.alpha=0.9;
}
#pragma mark -
#pragma mark Table view data source
-(NSInteger) numberofSectionInTableView:(UITableView*)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
    return [soundtitles count];
}
-(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 ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
                            {
                                   cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;


                            }

                                        }

  cell.textLabel.text=[soundtitles objectAtIndex:indexPath.row];
    cell.imageView.image=[UIImage imageNamed:[thumbs objectAtIndex:indexPath.row]];
    cell.backgroundColor=[UIColor clearColor];
    cell.textLabel.textColor= [UIColor whiteColor];
    return cell;

}

   #pragma mark -
   #pragma mark Table view delegate
    -(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath
  {
      NSString *selectedImage=[[NSString alloc]init];
      switch (indexPath.row) {
          case 0:
              selectedImage=@"novaposhta.jpg";
              break;
          case 1:
               selectedImage=@"ac-2.jpg";
              break;
          case 2:
               selectedImage=@"vugovskogo.jpg";
              break;
          case 3:
               selectedImage=@"naykova.jpg";
              break;
          case 4:
              selectedImage=@"iskra.jpg";
              break;
          default:
              break;
      }
      DetailViewController *detailScreen=[[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
      detailScreen.imageName=selectedImage;
      [self.navigationController pushViewController:detailScreen animated:YES];

  }

    - (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我还添加了 NavigatorController,但没有帮助。

【问题讨论】:

  • 欢迎您!如果您希望您的问题可以回答,则必须添加更多细节。就目前而言,不清楚你在问什么。您从未描述过您期望发生的事情,而是描述了正在发生的事情。
  • 为什么对 nsstring 使用 alloc
  • 你的问题没问题。在这之后会发生什么
  • 在pushViewController前添加nslog,检查detailScreen是否为nil。
  • 您在使用导航控制器吗?如何?在哪里?

标签: ios objective-c oop uitableview


【解决方案1】:

如果您正确复制了代码,原因如下:

 -(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath

tableViewdidSelectRowAtIndexPath 之间必须有空格

【讨论】:

  • 如何在 DetailViewController 中保存我的值?我只想添加一次值,关闭此值后打开我的应用程序的时间将在此文本字段中。谢谢。
  • 您应该查看NSUserDefaultsNSDictionary's writeToFile:atomically: 甚至CoreData。但这是另一个问题。
【解决方案2】:

改正方法如下:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 你的代码 }

代替你的方法

-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath

您只是忘记添加空格检查两种方法

【讨论】:

    猜你喜欢
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    相关资源
    最近更新 更多