Ios 目标c:当您单击第一个单元格时,会出现一个问题,其中选择了最后一个单元格

Ios 目标c:当您单击第一个单元格时,会出现一个问题,其中选择了最后一个单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,目前,总共创建了三个单元格。奇怪的是,当您单击第一个单元格时,最后一个单元格被单击 要检查此部分中的日志,请执行以下操作: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 我通过写下面的日志来检查我点击的单元格 NSIndexPath *c = [_tableView indexPathForSelectedRow]; NSLog(@"LOG 3 : %@"

目前,总共创建了三个单元格。奇怪的是,当您单击第一个单元格时,最后一个单元格被单击

要检查此部分中的日志,请执行以下操作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
我通过写下面的日志来检查我点击的单元格

NSIndexPath *c = [_tableView indexPathForSelectedRow];
NSLog(@"LOG 3 : %@", c);
第一个单元格单击:

LOG 3 : <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
LOG 3 : <NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}
我认为在这一部分创建单元的过程是错误的

目前我正在创建一个单元格,如下所示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    TableViewCell *cell;

    for (int i = 0 ; i < fileCount; i++) {
        indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];

        cell.fileName.text = [NSString stringWithFormat:@"%@",[_fileList[i] valueForKey:@"name"]];
        cell.fileDate.text = [NSString stringWithFormat:@"%@",[_fileList[i] valueForKey:@"date"]];
        cell.fileSize.text = [NSString stringWithFormat:@"%@",[_fileList[i] valueForKey:@"size"]];
    }

    return cell;
}
@interface ViewController : UIViewController<UITableViewDataSource,
UITableViewDelegate>
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
TableViewCell*单元格;
for(int i=0;i
我这样做的原因是_fileList数组包含多个具有名称、日期和大小对的字典 所以我们使用for语句创建了一个单元格


出于某种原因,当您单击第一个单元格时,最后一个单元格是否会单击?

应采用UITableViewDataSourceUITableViewDelegate协议和ViewController。h应如下所示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    TableViewCell *cell;

    for (int i = 0 ; i < fileCount; i++) {
        indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];

        cell.fileName.text = [NSString stringWithFormat:@"%@",[_fileList[i] valueForKey:@"name"]];
        cell.fileDate.text = [NSString stringWithFormat:@"%@",[_fileList[i] valueForKey:@"date"]];
        cell.fileSize.text = [NSString stringWithFormat:@"%@",[_fileList[i] valueForKey:@"size"]];
    }

    return cell;
}
@interface ViewController : UIViewController<UITableViewDataSource,
UITableViewDelegate>

应采用UITableViewDataSourceUITableViewDelegate协议和ViewController。h应如下所示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    TableViewCell *cell;

    for (int i = 0 ; i < fileCount; i++) {
        indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];

        cell.fileName.text = [NSString stringWithFormat:@"%@",[_fileList[i] valueForKey:@"name"]];
        cell.fileDate.text = [NSString stringWithFormat:@"%@",[_fileList[i] valueForKey:@"date"]];
        cell.fileSize.text = [NSString stringWithFormat:@"%@",[_fileList[i] valueForKey:@"size"]];
    }

    return cell;
}
@interface ViewController : UIViewController<UITableViewDataSource,
UITableViewDelegate>

请学习了解表视图数据源的工作原理
cellForRowAtIndexPath
被多次调用–对于分别通过实际索引路径的每一行。不要使用
valueForKey
为字典中的键获取值,除非您可以解释为什么明确需要KVC。在一个
cellforrowatinexpath
中创建所有单元格不是您的责任。每次为每个
indepath
调用此委托方法。您的责任只是定义单元格,并根据
indepath
对其进行自定义,然后返回。不多不少。谢谢你的回复。我想我需要学习很多关于UITableView的知识。请学习了解表视图数据源是如何工作的
cellForRowAtIndexPath
被多次调用–对于分别通过实际索引路径的每一行。不要使用
valueForKey
为字典中的键获取值,除非您可以解释为什么明确需要KVC。在一个
cellforrowatinexpath
中创建所有单元格不是您的责任。每次为每个
indepath
调用此委托方法。您的责任只是定义单元格,并根据
indepath
对其进行自定义,然后返回。不多不少。谢谢你的回复。我想我需要学习很多关于UITableView的知识。谢谢你的回复。在UICollectionView中,如果没有相同的代码(cell==nil),这不重要吗?我目前只使用这个。静态NSString*cellIdentifier=@“GridViewCell”;GridViewCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];cell.deviceName.text=[NSString stringWithFormat:@“%@”,[\u deviceList[indexPath.row]objectForKey:@“name”];cell.hostName.text=[NSString stringWithFormat:@“%@”,[[u deviceList[indexath.row]objectForKey:@“hostName”];返回单元;谢谢你的回复。在UICollectionView中,如果没有相同的代码(cell==nil),这不重要吗?我目前只使用这个。静态NSString*cellIdentifier=@“GridViewCell”;GridViewCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];cell.deviceName.text=[NSString stringWithFormat:@“%@”,[\u deviceList[indexPath.row]objectForKey:@“name”];cell.hostName.text=[NSString stringWithFormat:@“%@”,[[u deviceList[indexath.row]objectForKey:@“hostName”];返回单元;