And*_*kha 53 objective-c uitableview multiple-columns ios
我已经阅读了苹果文档,这对于Objective-C像我这样的初学者来说是不可理解的.我正在尝试UITableView按照这个 链接示例实现多列,它只是不起作用所以我需要理解如何cellForRowAtIndexPath工作,因为我个人这个方法似乎相当复杂.
1)它返回什么?UITableViewCell?但为什么它看起来如此奇怪?
-(UITableViewCell *)tableView:(UITableView *)tableView
Run Code Online (Sandbox Code Playgroud)
2)如何调用它,更重要的是如何将它连接到某个UITableView??? 如果我有两个UITableView名字firstTableView并且secondTableView我希望它们不同(表现cellForRowAtIndexPath不同)怎么办?我UITableViews该如何将自己与此联系起来
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
方法接受NSIndexPath,而不是UITableView.我该怎么办?
jba*_*100 96
我会尝试将其分解(例如来自文档)
/*
* The cellForRowAtIndexPath takes for argument the tableView (so if the same object
* is delegate for several tableViews it can identify which one is asking for a cell),
* and an indexPath which determines which row and section the cell is returned for.
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/*
* This is an important bit, it asks the table view if it has any available cells
* already created which it is not using (if they are offScreen), so that it can
* reuse them (saving the time of alloc/init/load from xib a new cell ).
* The identifier is there to differentiate between different types of cells
* (you can display different types of cells in the same table view)
*/
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
/*
* If the cell is nil it means no cell was available for reuse and that we should
* create a new one.
*/
if (cell == nil) {
/*
* Actually create a new cell (with an identifier so that it can be dequeued).
*/
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
/*
* Now that we have a cell we can configure it to display the data corresponding to
* this row/section
*/
NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"mainTitleKey"];
cell.detailTextLabel.text = [item objectForKey:@"secondaryTitleKey"];
NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"imageKey"] ofType:@"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;
/* Now that the cell is configured we return it to the table view so that it can display it */
return cell;
}
Run Code Online (Sandbox Code Playgroud)
这是一个DataSource方法,所以它将被调用在任何一个对象声明自己DataSource的UITableView.当表视图实际需要在屏幕上显示单元格时,会根据行数和节数(在其他DataSource方法中指定)调用它.
Mad*_*nRP 37
1)该函数返回一个表视图的单元格是吗?因此,返回的对象是类型UITableViewCell.这些是您在表格行中看到的对象.对于表视图,此函数基本上返回一个单元格.但是你可能会问,函数如何知道哪一行返回哪一行,这在第二个问题中得到了回答
2)NSIndexPath基本上是两件事 -
因为您的表可能被划分为多个部分,每个部分都有自己的行,这NSIndexPath将有助于您准确识别哪个部分和哪个行.它们都是整数.如果你是初学者,我会说只试一个部分.
如果UITableViewDataSource在视图控制器中实现协议,则会调用它.一种更简单的方法是添加一个UITableViewController类.我强烈推荐这个,因为Apple为您编写了一些代码,可以轻松实现可以描述表格的功能.无论如何,如果您选择自己实现此协议,则需要创建一个UITableViewCell对象并将其返回到任何行.看看它的类参考,以了解可重用性,因为表视图中显示的单元格一次又一次地重复使用(这是一个非常有效的设计btw).
至于何时有两个表视图,请查看方法.表视图传递给它,所以你不应该有这个问题.
基本上它是设计你的单元格,为每个单元格调用cellforrowatindexpath,单元格编号由indexpath.row和section number by indexpath.section找到.在这里,您可以使用标签,按钮或文本图像,以便为表中的所有行更新所需的任何内容.第二个问题的答案在索引路径的行的单元格中使用if语句
在目标C中
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(tableView == firstTableView)
{
//code for first table view
[cell.contentView addSubview: someView];
}
if(tableview == secondTableView)
{
//code for secondTableView
[cell.contentView addSubview: someView];
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
在Swift 3.0中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
if(tableView == firstTableView) {
//code for first table view
}
if(tableview == secondTableView) {
//code for secondTableView
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
100455 次 |
| 最近记录: |