简体   繁体   English

自定义UITableViewCell为零

[英]Custom UITableViewCell is nil

I am using xib to load the cell in the UITableViewController . 我正在使用xib加载UITableViewController的单元格。 There is a button on the cell by hitting which a modal pops up and when we hit something on that modal, I want to change the image in the cell. 击中单元格上有一个按钮,弹出一个模态,当我们点击该模态上的某个东西时,我想更改单元格中的图像。 But I am not able to get the cell even if I am passing the indexPath. 但是,即使我通过了indexPath,也无法获取该单元格。 I have used following code: 我使用了以下代码:

    NSIndexPath* indexPath1 = [NSIndexPath indexPathForRow:sender inSection:0];
    [self.tableView2 reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath1, nil] withRowAnimation:UITableViewRowAnimationNone];

I have written the code in the cellForRowAtIndexPath as follows: 我已经在cellForRowAtIndexPath编写了如下代码:

    if([[feedbackDictionary objectForKey:[NSString stringWithFormat:@"Key%li",indexPath.row]] isEqualToString:[NSString stringWithFormat:@"%li",indexPath.row]])

            {
                NSLog(@"Yay! Feedback");
                cell.checkmark.image=[UIImage imageNamed:@"tick"];
            }
            else
            {
                NSLog(@"Oops! No feedback");
                cell.checkmark.image=[UIImage imageNamed:@"orangetick"];
            }

I am supposing that when reloadrows will run,it will reload that particular cell by calling cellForRowAtIndexPath . reloadrows重新加载行将运行时,它将通过调用cellForRowAtIndexPath来重新加载特定的单元格。 But it is of no help. 但这没有帮助。 The image is not changing at all. 图像完全没有改变。

Secondly, I tried to get the cell by using following 其次,我尝试使用以下方法获取单元格

    NSIndexPath *ip=[NSIndexPath indexPathForItem:button.tag inSection:0];
    ExpandingCell *cell=(ExpandingCell*) [_tableView2 cellForRowAtIndexPath:ip];
    if(cell==nil)
    {
     NSLog(@"No cell");
    }

I am getting no cell as output. 我没有输出任何单元格。 Please help. 请帮忙。

At first, to debug it try to call [self.tableView2 reloadData] . 首先,要对其进行调试,请尝试调用[self.tableView2 reloadData] If your image is changed, it means that you were called wrong indexPath for reloadRowsAtIndexPaths method. 如果更改了图像,则意味着reloadRowsAtIndexPaths方法被称为错误的indexPath。 Also button.tag could return wrong tag, because cells are reusable in UITableView, and it can be cell that already used before. 同样, button.tag可能返回错误的标记,因为单元格可以在UITableView中重用,并且它可以是之前已经使用过的单元格。

I have found the problem and fixed it. 我已经找到问题并解决。 The problem was when modal pops up,it was loosing control over the view controller and that is why tableview is null and nothing was changed. 问题是当模式弹出时,它失去了对视图控制器的控制,这就是为什么tableview为null并且什么都没改变的原因。

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

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