ios - 未调用 cellForRowAtIndexPath - 使用标记来识别 TableView

标签 ios objective-c uitableview

我见过很多人问过类似的问题,但他们问题的答案并不是我的答案。

1) 我创建了一个带有空 View Controller 的单 View 应用程序。在那里,我拖动了一个新的 TableView (样式为 Plain),其中包含一个样式为 Basic 的原型(prototype)单元格。

2) 我正在尝试了解如何动态更改 TableView 的行为,因此我有一个名为 sectionRows 的可变数组,它将包含每个部分的行数。目前,具有多行的单个部分将是一项成就:)

3) 在我的 ViewController.h 中我设置了委托(delegate)

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@end

我还通过控制从 TableView 拖动到 ViewController Yellow-Circle 并设置了数据源和委托(delegate) socket 。

4) 在我的 ViewController.m 中,我定义了一些全局变量

@interface ViewController ()
{
    NSMutableArray *sectionRows;
    UITableView *myTableView;
}

第一个是我的数据数组(包含每个部分的行数,第二个是指向我的 TableView 的指针,我使用数字 View 标记“1”标识了它。

5)在我的 viewDidLoad 中,我初始化了所有内容:

- (void)viewDidLoad
{
    [super viewDidLoad];

    myTableView = (UITableView *)[self.view viewWithTag:1];

    sectionRows = [[NSMutableArray alloc] init]; // Create sectionarray

    [sectionRows addObject:[NSNumber numberWithInteger:1]];

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

    [myTableView reloadData];
}

如您所见,我什至确保再次设置数据源和委托(delegate),但这没有任何区别。

5) 我重载了 3 个方法。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"Returning %@ rows", [sectionRows objectAtIndex:section]);
    return (NSInteger)[sectionRows objectAtIndex:section]; // the number referenced in the array...
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"Returning %li sections", sectionRows.count);
    return sectionRows.count; // the size of the sectionRows array
}

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

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

    cell.textLabel.text = @"xxx";
    NSLog(@"Setting cell to %@", cell);
    return cell;
}

现在,当我运行此命令时,我收到 NSLog 返回确认信息,表明存在单个部分和单个行:

2014-07-27 19:58:34.599 TableViewTests[12877:60b] Returning 1 sections
2014-07-27 19:58:34.600 TableViewTests[12877:60b] Returning 1 rows

但是,正如您所看到的,cellForRowAtIndexPath 没有被调用。

我所看到的其他事情都没有表明我做错了什么。我正在做我认为我在另一个简单项目中成功完成的事情(学习),但我必须做一些不同的事情。

知道我遗漏了什么吗?

提前致谢

乔恩

最佳答案

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"Returning %@ rows", [sectionRows objectAtIndex:section]);
    return (NSInteger)[sectionRows objectAtIndex:section]; // the number referenced in the array...
}

这是不正确的。您不能将从数组中获得的内容转换为 NSInteger。这是一个指针。假设您将 NSNumber 存储到数组中:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"Returning %@ rows", [sectionRows objectAtIndex:section]);
    return [[sectionRows objectAtIndex:section] integerValue]; // the number referenced in the array...
}

关于ios - 未调用 cellForRowAtIndexPath - 使用标记来识别 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984514/

相关文章:

ios - 直接进入另一个 tableview 的子 segue?

ios - 越狱 iOS : Communication between tweak and application

ios - 在 TableView 未使用的空间添加 UIButton

ios - 检查 UITextField 是否有前导空格

ios - objective-c 正则表达式删除以空格和逗号开头的 NSString 字符

ios - 无法使用退格键从 iOS 中的文本字段中删除字符

ios - 在 Xcode 中订购代码片段

javascript - 如何使用 JavaScript 更改 UIWebView 字体?

ios - 文本换行后 UITextView 大小不会改变大小,直到键入第二个字符

iOS:如何使用整数值对 TableView 进行排序但想要显示字符串值?