【问题标题】:indexpath.row showing index 0 every timeindexpath.row 每次都显示索引 0
【发布时间】:2015-01-24 10:52:16
【问题描述】:

所以问题是,在我的 cellForRowAtIndexPath 方法中,每次 indexpath.row 仅为“0”。并确认我已经用“count”打印了计数器。但每次它显示 0 并且我的整个表都分配有相同的值。不知道怎么了。请有人帮忙。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [arr_Max count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableViewCell";
    count=(int)indexPath.row;

    NSLog(@"%d",count);
    SimpleTableViewCell *cell = (SimpleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.label.text=[NSString stringWithFormat:@"%@",[arr_Max objectAtIndex:count] ];
    return cell;
}

【问题讨论】:

  • 你们是如何实现 numberOfRowsInSection 的?
  • 请提供更多代码以了解您的问题。
  • 检查我编辑的版本
  • @Rahul 你是如何定义计数变量的?您是否在代码中的任何位置对其进行了初始化?
  • @Rahul 你已经返回 count fix 1 作为行数,那么你只能得到一个 0 的索引。

标签: ios uitableview row nsindexpath


【解决方案1】:

你的代码有误,正确的代码如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section
{
    return [arr_Max count];
}

【讨论】:

    【解决方案2】:

    很可能您的 tableView 委托方法 numberOfRowsInSection 返回了错误的行数。

    确保这是必需的委托方法,

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    

    返回一个有效数字 - 通常是数据源中的元素数。

    【讨论】:

    • 我猜这是因为在“numberOfRowsInSection”中只返回 1。我已经实现了这种方式,因为我想在单元格之间获得空间。请向我提出针对这种情况的适当解决方案。检查我编辑的版本
    【解决方案3】:

    您已使用以下内容更新您的代码,其他部分将保持不变:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [arr_Max count];
    }
    

    【讨论】:

    • 我已经实现了这种方式,因为我想在单元格之间获得空间。请为我提出适合这种情况的解决方案。
    • @Rahul 如果您想要单元格之间的空间,那么您可以在 SimpleTableViewCell 视图的底部放置更多空间。
    【解决方案4】:

    看来您有 number 或 rows = 1(来自 numberOfRowsInSection)。 所以每次,当你的 tableview 委托触发方法时,它只返回 1 行。 你能解释一下你的问题吗?

    按照我的理解,你应该继续这样的代码。

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: 
    (NSInteger)section
    {
    return [arr_Max count];
    } 
    

    那么您可能会根据您的数组内容获得计数值。 请评论是否可以或对此有任何疑问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 2020-06-12
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多