2012-01-11 105 views
0

誰能告訴我,我做錯了什麼? 我已經創建了一個快速項目,通過選擇模板「空應用」。 我創建了具有子類UITableViewController的新控制器。UITableViewController在xcode 4.2中無法正常工作?

我寫了一段代碼如下調用控制器:

-(BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
// self.window.backgroundColor = [UIColor whiteColor]; 

    MyTableViewController *mtvc = [[MyTableViewController alloc] init]; 

    UINavigationController *nav = [[UINavigationController alloc] init]; 
    [nav pushViewController:mtvc animated:YES]; 
    [self.window addSubview:nav.view]; 



    [self.window makeKeyAndVisible]; 
    return YES; 
} 

,並填寫了的UITableViewController的方法來檢查它是否工作:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"MyCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    cell.text = @"test"; 

    return cell; 
} 

當我嘗試選擇單元格表時,方法 didSelectRowAtIndexPath根本不會調用。

我是新的iphone編程,所以也許我做錯了什麼?


我已經發布問題here與創建tableviewcontroller

我已經進行了多試驗幾次,似乎ARC引起了我的問題。我創建了幾個項目,如:

  • 在控制器
  • 創建一個簡單的視圖表而不廈門國際銀行創建使用的UIViewController沒有廈門國際銀行
  • 創建使用的UIViewController與廈門國際銀行表視圖表視圖

我試圖向下滾動表格,每次程序都崩潰。 其次,我正確設置委託,但我不能使用方法didSelectRowAtIndexPath,它有線數據源方法運行良好。

這是當我試圖向下滾動表格單元格時,程序崩潰後的堆棧。

> (gdb) bt 
> #0 0x0156009b in objc_msgSend() 
> #1 0x07821800 in ??() 
> #2 0x000ad589 in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:]() 
> #3 0x00098dfd in -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:]() 
> #4 0x000a7851 in -[UITableView layoutSubviews]() 
> #5 0x00052322 in -[UIView(CALayerDelegate) layoutSublayersOfLayer:]() 
> #6 0x013bde72 in -[NSObject performSelector:withObject:]() 
> #7 0x01d6692d in -[CALayer layoutSublayers]() 
> #8 0x01d70827 in CA::Layer::layout_if_needed() 
> #9 0x01cf6fa7 in CA::Context::commit_transaction() 
> #10 0x01cf8ea6 in CA::Transaction::commit() 
> #11 0x01d9237a in CA::Display::DisplayLink::dispatch() 
> #12 0x01d921af in CA::Display::TimerDisplayLink::callback() 
> #13 0x01390966 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__() 
> #14 0x01390407 in __CFRunLoopDoTimer() 
> #15 0x012f37c0 in __CFRunLoopRun() 
> #16 0x012f2db4 in CFRunLoopRunSpecific() 
> #17 0x012f2ccb in CFRunLoopRunInMode() 
> #18 0x012a5879 in GSEventRunModal() 
> #19 0x012a593e in GSEventRun() 
> #20 0x00013a9b in UIApplicationMain() 
> #21 0x00002588 in main (argc=1, argv=0xbfffed80) at /Users/lsd/Development/iPhone/MyTableView/MyTableView/main.m:16 
> #22 0x000024e5 in start() (gdb) 

我用相同的代碼行創建了與未選中的ARC相同的項目,然後所有東西都像魅力一樣開始工作。

約翰

+0

確保正確設置委託,爲的UITableViewController。 self.tableView。委託=自我;並確保UITableViewController符合 2012-01-11 22:43:15

+0

您是否設置了表視圖的委託? – 2012-01-11 22:44:36

+0

我試圖在UITableViewController的方法viewDidLoad中設置委託。糾正我,如果我錯了,但通過在控制器中設置self.tableView.delegate是不是一個壞主意?不應該在視圖中以某種方式設置它?對我來說有點奇怪,dataSource工作正常。 感謝您的幫助。 約翰 – 2012-01-11 22:57:41

回答

0

我認爲ü錯過這個代表 - (NSInteger的)numberOfSectionsInTableView:(UITableView的*)的tableView { 返回1; //在實現代碼如下部分的數量; }

+0

問題是我應該在哪裏設置委託?我試圖在tableviewcontroller中的每個生命週期方法中都沒有成功。我認爲最好的方法是在xcode中重複我的步驟,並在你自己的眼睛中發現tableviewcontroller模板有問題。難道你不認爲蘋果公司給出一個簡單的模板忘了放一行讓代表工作,或沒有預見到有人可能會使用uitableviewcontroller模板沒有xib? – 2012-01-12 09:13:30

0

這是一個沒有xib的項目,並且在控制器內部創建了表。

@interface MyTableViewController : UIViewController 
<UITableViewDelegate,UITableViewDataSource> 

@property (nonatomic, strong) UITableView *myTableView; 

@end 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 

    self.myTableView.dataSource = self; 
    self.myTableView.delegate = self; 

    [self.view addSubview:self.myTableView]; 
} 
#pragma mark - UIViewTable DataSource methods 



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 100; 
} 



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *result = nil; 

    static NSString *CellIdentifier = @"MyTableViewCellId"; 

    result = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(result == nil) 
    { 
     result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    result.textLabel.text = [NSString stringWithFormat:@"Cell %ld",(long)indexPath.row]; 


    return result; 
}