【问题标题】:UITableViewCell indexPath.section issue on iPadiPad 上的 UITableViewCell indexPath.section 问题
【发布时间】:2014-01-17 15:52:16
【问题描述】:

我正在尝试在第 8 行的单元格文本旁边添加一个图像。我的问题是它没有进入(indexPath.row == 9) 方法,而我已经放置了断点,我从indexPath.row = {length = 2, path = 0 - 0} 获得了这个值。那么请问我的问题在哪里?

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  if (!cell)
  {
    cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"cell"] autorelease];

    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bakiyem-cell-bg.png"]];
    img.x = 10.;
    img.y = -6.;
    [cell.contentView addSubview:img];
    [img release];

    //
    cell.textLabel.font = [Constants regularFontWithSize:24.];
    cell.textLabel.textColor = [Constants textGrayColor];
    cell.detailTextLabel.font = [Constants mediumFontWithSize:24.];
}

cell.detailTextLabel.textColor = indexPath.row == 4 ? [UIColor colorWithRed:218./255. green:0 blue:37./255. alpha:1] : [Constants textGrayColor];

   //My issue
 if (indexPath.row == 9) {

        cell.imageView.image = [UIImage imageNamed:@"info-logo.png"];   
 }

//
NSDictionary *dic = [data objectAtIndex:indexPath.row];
cell.textLabel.text = [dic objectForKey:@"value1"];
cell.detailTextLabel.text = [dic objectForKey:@"value2"];

return cell;
}

【问题讨论】:

    标签: ios ipad uitableview


    【解决方案1】:

    应该是这样的

    if (indexPath.row == 7) {
    

    因为你只有一排。

    如果断点仍未进入您的 if 语句,则在其周围添加更多日志以查看发生了什么。

    如果你试试怎么样

    NSLog(@"row = %i", indexPath.row);
    if (indexPath.row == 7) {
        NSLog(@"In if statement for row = %i", indexPath.row);
        UIImage *myImage = [UIImage imageNamed:@"info-logo.png"];
        NSLog(@"My image=%@",myImage);
        cell.imageView.image = myImage;
    }
    

    NSLog 中显示了什么?

    【讨论】:

    • 从row=0到row=9
    • 那么你得到 row=7 吗?然后你应该输入 if 块。尝试在那里也使用 NSLog。
    • 它开始在if 语句中传递。我不知道为什么以及如何?
    • 因此,如果您更新以添加我刚刚在答案中添加的 NSLog - 您是否将该语句记录在 if 块中?
    • 是的,这很有趣!!
    【解决方案2】:
    • 八行的索引为 7
    • 您的意思可能是indexPath.row 而不是indexPath.section
    • 如果要自定义UITableViewCells,请使用自定义单元格

    【讨论】:

    • 我试过'indexPath.row',但没用。我现在也使用了断点,我得到了{length = 2, path = 0 - 0}
    • 我不想自定义整个单元格,我只想在其中一行中放一张图片。
    【解决方案3】:

    试试indexPath.section == 7。或者可能indexPath.row == 7 Cem 在评论中写道。

    【讨论】:

    • 我尝试了不同的数字,但没有任何图像。
    • @Cem 你有多少个部分?我想你的意思可能是 `if (indexPath.row == 9)`(或者 7,如 user2509601 所说。)
    • @Cem 你的配置如何 - -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    • @Cem 所以你必须使用indexPath.row == 7。然后首先在 if 语句中使用 NSLog 尝试它。因此,如果它进入 if 语句,您还可以借助断点检查 UIImage 是否不是 nil
    • 没有进入if statment :|
    【解决方案4】:

    您是在 1 节内有 9 行,还是 9 节在每个节中 1 行?

    对于第一种情况,请使用: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 返回 1 ; }

    • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 如果(部分==0) { 返回 9; } }

    对于第二种情况;

    • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 返回 9 ; }

      • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 返回 1; }

    另外,在 cellForRowAtIndexPath 方法中使用:

    在第一种情况下:indexPath.row==9 第二种情况:indexPath.section==0

    【讨论】:

      【解决方案5】:

      您应该在以下方法中至少有 9 行是数据数组,并且 indexPath.row = {length = 2, path = 0 - 0} 表示您在第 0 部分的第 0 行。如果您以行数方法返回硬代码 9,那么它将输入 (indexPath.row == 8)

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-01
        • 2013-11-08
        相关资源
        最近更新 更多