【问题标题】:Unable to view table in RootViewController无法在 RootViewController 中查看表
【发布时间】:2011-02-07 12:59:37
【问题描述】:

我正在开发一个导航应用程序,由于某种原因,它不允许我在初始屏幕上查看我的表格(即从 RootViewController)。我的“viewDidLoad”方法调用了以下方法:`

- (void) locationManager:(CLLocationManager *)manager 
 didUpdateToLocation:(CLLocation *)newLocation
        fromLocation:(CLLocation *)oldLocation {

` 它做了一些工作,然后调用方法:

- (void) readRestaurantsFromDatabase:(double)userLatitude 
                withUserLocation:(double)userLongitude{

此方法对名为“sortedArray”的 NSArray 做了一些工作,它是在 RootViewController 中声明和合成的属性:

//compile a list of categories
        NSMutableArray *categoryList = [[NSMutableArray alloc] init];
        [categoryList addObject:@"All Types"];

        for (Restaurant *restCat in restaurants){

            [categoryList addObject:restCat.category];
        }


        //remove duplicates 
        NSArray *copy = [categoryList copy];
        NSInteger index = [copy count] - 1;

        for (NSString *restCategory in [copy reverseObjectEnumerator]) {

            if ([categoryList indexOfObject:restCategory inRange:NSMakeRange(0, index)] != NSNotFound) {
                [categoryList removeObjectAtIndex:index];
            }

            index--;

        }

        [copy release];

        //put list in alphabetical order
        sortedArray = [categoryList sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

以上方法就这样结束了。然后我的“cellForRowAtIndexPath”方法有以下代码:

- (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] autorelease];
}

// Configure the cell.

NSString *cellValue = [sortedArray objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;

}

对我来说,一切看起来都很好。当我运行我的代码时,我有 NSLog 语句向控制台发出输出,这清楚地告诉我 NSArray sortedArray 包含数据。然而,当我在 XCode 中的 iPhone 模拟器上运行代码时,我得到一个空表。谁能看到我做错了什么?

提前感谢所有回复的人。

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:

    您是否已将 tableView 连接到界面构建器中的属性?

    此外,当您的排序数组发生变化时,您可能需要调用[myTabelView reloadData]; 来刷新表格。每次更改 sortedArray 时都执行此操作,即

    //put list in alphabetical order
    sortedArray = [categoryList sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    
    // The sorted array has changed, get the table view to refresh
    [myTabelView reloadData];
    

    【讨论】:

    • 非常感谢您的及时回复。我在哪里放置这段代码(即哪种方法)?是:cellForRowAtIndexPath 还是:readRowAtIndexPath?再次感谢您的帮助。保重。
    • 查看我的编辑示例。基本上,每次更改 tableView 显示的数据时,都需要告诉 tableView 它已更改。
    • 非常感谢您的及时帮助。你的解决方案奏效了!保重
    【解决方案2】:

    你实现了 tableView:numberOfRowsInSection: 吗?

    这应该可能返回 [sortedArray count]。

    【讨论】:

      【解决方案3】:

      你填写numbersOfRowInSection了吗?

      附带说明,我不明白为什么您要复制所有内容然后删除重复项,而不是在数组包含对象时跳过添加项。

      【讨论】:

        【解决方案4】:

        仔细检查您的 tableview 数据源属性。如果未为您的 UITableView 设置数据源。确保您将其设置为您的对象,并确保您的对象定义了 -(NSInteger)tableView:numberOfRowsInSection: 方法。

        这应该返回 [sortedArray count]。

        【讨论】:

          猜你喜欢
          • 2019-12-31
          • 1970-01-01
          • 2019-10-31
          • 2017-02-16
          • 1970-01-01
          • 2018-04-27
          • 1970-01-01
          • 1970-01-01
          • 2017-09-05
          相关资源
          最近更新 更多