UITableViewController не работает должным образом в xcode 4.2? - PullRequest
0 голосов
/ 12 января 2012

Может кто-нибудь сказать мне, что я делаю что-то не так? Я создал быстрый проект, выбрав шаблон 'Пустое приложение' . Я создал новый контроллер с подклассом UITableViewController .

Я написал ниже кусок кода для вызова контроллера:

</p>

<pre><code>-(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, чтобы проверить, работает ли он:

</p>

<pre><code>- (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, так что, может быть, я что-то не так делаю?


Я опубликовал проблему здесь с созданием tableviewcontroller

Я провел еще несколько тестов, и кажется, что ARC вызвал мои проблемы. Я создал несколько проектов, таких как:

  • создание таблицы простого вида без xib в контроллере
  • создание табличного представления с использованием uiviewcontroller без xib
  • создание табличного представления с помощью uiviewcontroller с xib

Я пытался прокрутить таблицу вниз, в каждом случае программа вылетала. Во-вторых, я правильно установил делегат, но не могу использовать метод 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, затем все начинает работать как чудо.

John

Ответы [ 2 ]

0 голосов
/ 13 января 2012

Это проект без 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;
}
0 голосов
/ 12 января 2012

я думаю, что вы пропустили этого делегата - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView { return 1; // Номер раздела в табличном представлении; }

...