objective-c - 使用数组传递数据但在 TableView 委托(delegate)中显示为空

标签 objective-c ios uitableview ios4 nsarray

我的应用基于 UISplitViewController,它是一个通用应用。
我的应用程序中有这段代码,它确实可以从网络服务器查找数据。

结果显示计数为 100,但 [myTable reloadData] 之后 NSMutableArraynil

我该如何解决?

场景 Page1:一个搜索引擎,将值传递给另一个 View Page2:从 [Page1] 接收值,然后传递给实例 sendData。通过 JSON 获取结果,然后将它们转换为 NSMutableArray',使用此NSMutableArrayviewdidload

之后填充UITableView'

代码:

- (void)configureView
{
    if (self.detailItem) {
        NSLog(@"ConfigureView");
        [self sendData];
        [self refreshTable];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"viewdidload");
    [self configureView];
}

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

-(void) refreshTable 
{
    NSLog(@"Refresh Table");
    [myTable reloadData];
    NSLog(@"Counter:%i",allFilteredItemsArray.count);
}

#pragma mark - Table

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection: %i", allFilteredItemsArray.count);
    return [allFilteredItemsArray count];
}

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"fileredCell";
    UITableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:CellIdentifier];
    HardwareCell *hardwareCell = [[HardwareCell alloc] init];

    // Configure the cell...
    NSDictionary *item = [allFilteredItemsArray objectAtIndex:[indexPath row]];
    hardwareCell.lbl_model = [item objectForKey:@"name"];

    return cell;

}

#pragma mark - Searching Asset

-(void)sendData
{    
    NSString *searchString = [self.detailItem description];

    .........ignored some of the code............

    allFilteredItemsArray = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSLog(@"Search Count: %i", allFilteredItemsArray.count);
    NSLog(@"End Searching Data.");

}

结果:

2012-10-17 10:45:54.535 Portal[10753:c07] viewdidload
2012-10-17 10:45:54.536 Portal[10753:c07] Value to be sent: %/%/%/%/%/1
2012-10-17 10:45:54.537 Portal[10753:c07] ConfigureView
2012-10-17 10:45:54.537 Portal[10753:c07] Start Seraching Data...
2012-10-17 10:45:54.923 Portal[10753:c07] Search Count: 100
2012-10-17 10:45:54.923 Portal[10753:c07] End Searching Data.
2012-10-17 10:45:54.923 Portal[10753:c07] Refresh Table
2012-10-17 10:45:54.924 Portal[10753:c07] Counter:100
2012-10-17 10:45:54.924 Portal[10753:c07] numberOfRowsInSection: 0

最佳答案

虽然它是类级对象,但您需要强制分配您的数组。在 viewdidload 中分配 allFilteredItemsArray 并将其写为 self.allFilteredItemsArray。检查以下代码:

-(无效)viewDidLoad {

[ super viewDidLoad];

NSMutableArray *tempArray=[[NSMutableArray alloc] init];

self.allFilteredItemsArray=tempArray;

[tempArray 释放];

NSLog(@"viewdidload");

[自行配置View];

您也可以使用 %d 作为数组计数,如下所示:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSLog(@"numberOfRowsInSection: %d",[allFilteredItemsArray count]);

返回 [allFilteredItemsArray 计数];

关于objective-c - 使用数组传递数据但在 TableView 委托(delegate)中显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12926436/

相关文章:

iphone - 如何为特定文件添加 ARC?

ios - 尝试将文本附加到 Swift 循环内的属性

ios - 无法将类型 'NSAttributedString.Key' 的值转换为预期的字典键类型 'String' 错误 (swift4.2)

ios - 在 ios 中单击 + 或 - 按钮时在标签上递增、递减

ios - Swift 4 - 尝试在 AVPlayerViewController 中从后端显示 AWS 视频 url 不起作用,出现奇怪的错误

objective-c - ObjC/ cocoa : Get fill color property of current graphics context

objective-c - 创建好友属性的正确方法

iphone - 带有 Core Animation 的 iPad 翻页时钟

ios - UITextField 上的 setDefaultTextAttributes 不起作用

iphone - iOS - UITableView 在不同的 View Controller 中将 URL 推送到 UIWebView