简体   繁体   English

indexpath.row显示的行数更少

[英]indexpath.row is showing less number of rows

I have created an tableview with an array. 我用数组创建了一个tableview There are 7 items in my array. 我的阵列中有7个项目。 But in cellForRowAtIndexPath method i'm unable to get indexpath.row == 6 . 但是在cellForRowAtIndexPath方法中,我无法获取indexpath.row == 6 What could be the reason. 可能是什么原因。 Code is below for this. 代码在下面。

  -(void)viewDidLoad
    {
      [super viewDidLoad];

      self.menu = [NSArray arrayWithObjects:@"Home",@"ansCategory",@"askQue",@"myQue",@"likedQue", @"topQue", @"topUser",nil];             
    }

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     {
       // Return the number of sections.
       return 1;
     }

      -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
        // Return the number of rows in the section.
        // NSLog(@"count::::::%d",self.menu.count);
           return self.menu.count;
     }
       -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier = @"Cell";    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle                   reuseIdentifier:cellIdentifier];

        NSLog(@"rows:%@",indexPath);
        NSLog(@"row:%@",[self.menu objectAtIndex:indexPath.row]);
    }
    return cell;

 }

I'm getting following out put: 我正在关注以下内容:

[here] ( https://www.dropbox.com/s/pz6t34xr7l4glxz/menu1.png ) [这里]( https://www.dropbox.com/s/pz6t34xr7l4glxz/menu1.png

first index is overlaying on last one. 第一个索引覆盖在最后一个索引上。

The cell is reused. 该单元被重用。 Put your log statement outside of the if(cell=nil) : 将您的日志语句放在if(cell=nil)

Here is the working example for outputting the contents of the array in a tableview. 这是在表视图中输出数组内容的工作示例。 Item with index 6 which is the seventh in the array ("topUser") is in red. 索引为6(数组中的第七个)的项目(“ topUser”)为红色。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    NSLog(@"rows:%@",indexPath);
    NSLog(@"row:%@",[self.menu objectAtIndex:indexPath.row]);

    cell.textLabel.text = self.menu[indexPath.row];

    if (indexPath.row == 6)
        cell.textLabel.textColor = [UIColor redColor];        
    return cell;
}

在此处输入图片说明

For N items the last indexpath is (N-1) 对于N个项目,最后一个索引路径是(N-1)

Thus, if you have 10 items, then the index path of last item is 9 because the indexpath of 1st item in array is 0. 因此,如果您有10个项目,则最后一个项目的索引路径为9,因为数组中第一个项目的索引路径为0。

indexPath of items starts with 0 and not 1. 项目的indexPath以0而不是1开头。

Try to access the index path in your answer like below in your code 尝试像下面的代码一样访问答案中的索引路径

Instead of : 代替 :

if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle                   reuseIdentifier:cellIdentifier];

        NSLog(@"rows:%@",indexPath);
        NSLog(@"row:%@",[self.menu objectAtIndex:indexPath.row]);
    }

Write

    if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle                   reuseIdentifier:cellIdentifier];
 }
    //Access the indexPath outside here... 
    //Because once the cells are created and not nil 
    //then the above scope will be bypassed.
    NSLog(@"rows:%@",indexPath);
    NSLog(@"row:%@",[self.menu objectAtIndex:indexPath.row]);
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

每当需要创建新行并将其添加到tableview时都会执行.tableview会填充行,以便需要在屏幕上支付时通过调用完全相同的方法来创建它,因此如果第7行不可见在屏幕上,该方法不会针对第7行执行,即indexpath.row 6,因此要获取记录第7行的方法,只需向下滚动即可使表格可见该单元格,它将执行该方法,您可以有日志

我将代码更改为以下代码,它可以正常工作。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM