【问题标题】:my tableview isn't being called wont crash at breakpoint我的 tableview 没有被调用不会在断点处崩溃
【发布时间】:2013-01-06 07:04:14
【问题描述】:

好的,所以我一直在摆弄一些 iOS 开发,我对此很陌生。通常更多的是 PHP 和 JavaScript 人。但这是我的问题......在我的应用程序中,我正在创建从本地 xml 文件中提取好友列表(它将指向服务器上生成用户好友列表的 php 页面,但为了调试和开发的简单起见,我是使用本地。)

我有一个视图控制器(storybaord),当我点击“朋友”时加载,截至目前,视图加载但没有数据,在调试器中我可以看到它从 XML 中提取数据,所以我想,好吧,也许 TableView 永远不会被调用,所以我在

上放了一个断点
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

但是应用还是继续加载页面,

回顾一下,我有一个视图控制器,有一个搜索栏、工具栏和一个表格视图,表格视图有一个占位符单元格,给定标识符 Cell,这里是 friendsListTableViewController.h 的编码

//
//  friendsListTableViewController.h
//  @ME
//
//  Created by Aaron Russell on 1/22/13.
//  Copyright (c) 2013 Aaron Russell. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "TBXML.h"
@interface friendsListTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, NSObject>{
    NSMutableArray *friendList;
    TBXML * tbxml;
    IBOutlet UIImage *imageFile;
}
@property (nonatomic, strong) NSMutableArray *_friends;
@property (nonatomic, strong) NSString *lname;

@end

这里是 friendsListTableViewController.m 文件

//
//  friendsListTableViewController.m
//  @ME
//
//  Created by Aaron Russell on 1/22/13.
//  Copyright (c) 2013 Aaron Russell. All rights reserved.
//

#import "friendsListTableViewController.h"

@interface friendsListTableViewController ()

@end

@implementation friendsListTableViewController


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

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

- (void)viewDidLoad {
    [super viewDidLoad];

    //USED TO CONNECT TO SERVERS XML
    //    NSData *xmlData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"www.soontobesite.com"]];
    //tbxml = [[TBXML alloc]initWithXMLData:xmlData];
    NSString *xmlData = [[NSBundle mainBundle] pathForResource:@"friendlist" ofType:@"xml"];
    NSData *data = [[NSData alloc] initWithContentsOfFile:xmlData];
    tbxml = [[TBXML alloc]initWithXMLData:data];

    //strings
    // Obtain root element
    TBXMLElement * root = tbxml.rootXMLElement;
    if (root)
    {
        TBXMLElement * elem_PLANT = [TBXML childElementNamed:@"friend" parentElement:root];
        while (elem_PLANT !=nil)
        {
            TBXMLElement * elem_BOTANICAL = [TBXML childElementNamed:@"fname" parentElement:elem_PLANT];
            NSString *botanicalName = [TBXML textForElement:elem_BOTANICAL];
            [friendList addObject:botanicalName];
            elem_PLANT = [TBXML nextSiblingNamed:@"friend" searchFromElement:elem_PLANT]; //IF I CALL BREAKPOINT ON THIS LINE THE SIMULATOR BREAKS
        }

        //TBXMLElement *fname = [TBXML childElementNamed:@"fname" parentElement:elem_PLANT];
    }

}


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [friendList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [friendList objectAtIndex:indexPath.row];

    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

//编辑//

感谢下面的 cmets 中列出的一些建议,在它不会在 numberOfSectionsInTableView 或 numberOfRowsInSection 处中断之前,我走得更远,但现在当我在 numberOfSectionsInTableView 或 numberOfRowsInSection 上设置中断但仍不是 cellForRowAtIndexPath 时,它会中断

【问题讨论】:

  • 您是否在情节提要中正确链接了表视图控制器以作为表视图委托和数据源?
  • 确保你已经在 storyboard 中链接了 tableview 的委托和数据源。我可以在代码中看到您正在实现两者。
  • 谢谢你们俩的回答,我照你说的做了,我走得更远了,现在当我在 numberOfSectionsInTableView 或 numberOfRowsInSection 上打断但仍不是 cellForRowAtIndexPath 时,它会打断

标签: objective-c tableview subclass breakpoints tbxml


【解决方案1】:

您忘记分配 friendList 实例变量。在viewDidLoad 的开头(或者在初始化器中更好)执行以下操作:

friendList = [[NSMutableArray alloc] initWithCapacity:0];

如果friendList 未初始化,则它具有nil 值。在 Objective-C 中向nil 发送消息不会使应用程序崩溃,它只是不做任何事情。另见this SO question

在您的代码中,调用 [friendList addObject:botanicalName] 根本没有任何作用。稍后,当您在numberOfRowsInSection 中调用[friendList count] 时,您将向表视图返回0(零)。由于表视图认为没有行,它永远不会调用cellForRowAtIndexPath

【讨论】:

  • 哇!你是最好的!现在已经花了 2 天半的时间来尝试获取这些数据。非常感谢我的朋友。
  • 也感谢您提供的链接,对我将来有很大帮助(:
  • @AaronRussell 很高兴我能帮上忙。这不会是您最后一次遇到这个特殊的陷阱,但希望下次您能更快地发现问题。
猜你喜欢
  • 2014-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
相关资源
最近更新 更多