我是 Objective-C 编程语言的新手。我创建了一个表并创建了表 View 的所有方法。但我不了解 CellForRowAtIndexPath。请告诉我它是如何工作的。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
这是 UITableView 的委托方法。返回的对象是 UITableViewCell 类型。这些是您在表的行中看到的对象。NSIndexPath 有两个 this Section 和 Row。
如果您在视图控制器中实现 UITableViewDataSource 协议,则会调用它。一种更简单的方法是添加一个 UITableViewController 类。我强烈推荐这个,因为 Apple 为您编写了一些代码,可以轻松实现可以描述表的功能。无论如何,如果你选择自己实现这个协议,你需要创建一个 UITableViewCell 对象并为任何行返回它。查看它的类参考以了解可重用性,因为表视图中显示的单元格会一次又一次地重用(顺便说一句,这是一个非常有效的设计)。
如果您实现自定义单元格,那么我强烈建议您使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
仅返回此处未设置的空单元格。使用类似的东西
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCart_Cell" forIndexPath:indexPath];
return cell;
}
并使用这个委托,它将在 cellForRow 数据源方法之后调用
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell isKindOfClass:[CartTableViewCell class]])
{
CartTableViewCell *cell1 = (CartTableViewCell*)cell;
MyCartModel* data = [_myCartProductArray objectAtIndex:indexPath.row];
[cell1 setUpData:data];
}
}
并在 UITableviewcell 自定义类中设置 UILabel 上的数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句