Dav*_*est 1 uitableview uiviewcontroller nsindexpath ios uistoryboard
男装,
我需要一些帮助.这几乎就像我知道我正在尝试做什么,但在编码时我一直无法使它工作.场景是我有一个视图控制器,其中包含一个表视图.我的表视图有三种不同的动态原型,因为我有三种不同的单元格.现在,我想要做的就是指定在哪一行中生成哪个原型单元格.我还在故事板中给出了每个原型单元的唯一标识符.
我理解的方法是我的cellforRowatIndexpath方法需要了解在该行中显示哪种类型的单元格,然后选择标识符,出列并根据需要设置内容.
相应地,这是我的代码,我正在尝试做的事情:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath * )indexPath
{
if(indexPath.row==0)
{
static NSString *CellIdentifier = @"HelloCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// configure your cell here...
return cell;
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,事情没有按计划运行,我的应用程序崩溃给了我一个2013-02-07 16:47:39.859 ProjectsABC30068:c07] *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [NSIndexPath setTableViewStyle:] :无法识别的选择器发送到实例0x83812e0'错误.
由于我已正确设置数据源和委托,因此表视图没有问题.并且还为表格视图制作了一个出口.
我还尝试将cellforRowAtIndexPath中的if语句更改为if(myTable.indexPathForSelectedRow == 0).这会在所有单元格中生成动态原型,但至少应用程序不会停止.
你们认为这是什么问题?
我不知道如何正确使用索引路径,如果有人可以帮助我,我会很感激.
使用Storyboard和Dynamic原型单元,您不必担心或检查是否dequeueReusableCellWithIdentifier:
返回零单元格.
你要做什么:
dequeueReusableCellWithIdentifier:
我们有一个例子.假设您有三个原型单元格,标识符为"Cell1","Cell2"和"Cell3".并假设您有三行,每行都显示一个原型单元格.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:@"Cell%d", indexPath.row + 1];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
cell.textLabel.text = identifier; // just show the identifier as title
return cell;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2822 次 |
最近记录: |