ios - indexpath.row 显示的行数较少

标签 ios iphone objective-c uitableview

我创建了一个带有数组的tableview。我的数组中有 7 个项目。但是在 cellForRowAtIndexPath 方法中,我无法获得 indexpath.row == 6。可能是什么原因。 代码如下。

  -(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;

 }

我正在关注:

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

第一个索引覆盖在最后一个上。

最佳答案

细胞被重复使用。 将您的日志语句放在 if(cell=nil) 之外:

这是在 TableView 中输出数组内容的工作示例。数组中第七个索引为 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;
}

enter image description here

关于ios - indexpath.row 显示的行数较少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18201749/

相关文章:

iphone - 如何检查 UITableViewCells 是否为空白

ios - Objective-C prepareForSegue 问题

iphone - 尝试支持 3.5 和 4 英寸屏幕的 iPhone 时出现问题

c# - Xamarin.forms 中的动画 gif

ios - ItunesConnect 永远处理应用程序

ios - AVPlayer 音频静音

iphone - 通用应用如何区分iPhone和iPad?

iphone - 树屋

objective-c - iOS 第二屏幕

objective-c - 动画 CALayer 阴影路径