1

谁能帮我理解我做错了什么UITableView

我已经设置了一个包含两个自定义单元格的消息列表 - 取决于变量是真还是假。我想将我的数据应用到相关的单元格 - 但我得到的是两个自定义单元格在另一个之上重复 3 次,如下所示(前景中的单元格没有设置样式 - 只是举例!)

在此处输入图像描述

这是我的代码 -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    static NSString *CellIdentifierRead = @"CellRead";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier  forIndexPath:indexPath];

    customCellRead *cellRead = [tableView dequeueReusableCellWithIdentifier:CellIdentifierRead forIndexPath:indexPath];

    notifications *n = [self.GPTNotifications objectAtIndex:indexPath.row];

    if (n.read == false) {
        cellRead.readText.text =n.notifMessage;
        cellRead.reDate.text =n.notifDateD;
        cellRead.resub.text = n.notifDateD;

    }
    if (n.read == true) {


       cell.notifTitle.text = n.notifTitleD;
       cell.notifDate.text = n.notifDateD;
       cell.notifMsg.text = n.notifMessage;
    }

// Configure the cell...

    return cell;
    return cellRead;
}
4

3 回答 3

3

首先我们必须了解 UITableView 是如何工作的。使用委托模式,它向控制器询问它必须在给定索引处显示哪个单元格。

想到的第一种方法是在 init 或 viewDidLoad 方法中初始化一个 UITableViewCell 实例数组,并在 tableView:cellForRowAtIndexPath 处提供良好的单元格:

基本上

- (void)viewDidLoad
self.cells = @[myCell1, myCell2];

- (UITableViewCell *)tableView:cellForRowAtIndexPath:index
return self.cells[index.row];

但是你必须记住,列表被设计成可能包含大量的单元格。所以苹果设计了一种方法来做到这一点。操纵实体总是比操纵 UI 元素更好。因此,Apple 为您提供了一种“回收”细胞的方法。在给定时间,屏幕上的单元格少于 10 个。

因此,当一个单元格离开屏幕时,tableView 会将其保存在内存中以备后用。当一个单元格出现在屏幕上时,您可以使用dequeueReusableCellWithIndentifier:@"MyCell". 一开始在队列中找不到单元格,因此它将返回 nil。所以你必须初始化一个单元格。

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//Do initial visual configuration here
cell.textLabel.textColor = [UIColor redColor];
}
// Get entity 
Notification entity = self.GPTNotifications[indexPath.row];
// Configure the according
cell.textLabel.text = entity.x;

return cell;

因此,对于您的确切问题,我们首先要知道的是:根据读取属性,您是否有 2 个不同的 tableView 或 2 种单元格?

对于第二个问题,您的方法应该是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    notifications *n = [self.GPTNotifications objectAtIndex:indexPath.row];
    CustomCell *cell;
    if (n.read)
    {
            static NSString *CellIdentifierRead = @"CellNotifRead";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierRead];
        if (cell == nil)
            cell = [[CustomCell alloc] init];

        cell.notifTitle.text = n.notifTitleD;
        cell.notifDate.text = n.notifDateD;
        cell.notifMsg.text = n.notifMessage;
    }
    else
    {
            static NSString *CellIdentifier = @"CellNotifUnread";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
            cell = [[CustomCell alloc] init];

        cellRead.readText.text =n.notifMessage;
        cellRead.reDate.text =n.notifDateD;
        cellRead.resub.text = n.notifDateD;
    }
    return cell;
}
于 2013-10-11T09:37:23.737 回答
2
  1. 您不能使用 2 个 return 语句,只会执行第一个;

  2. 您应该添加检查您的单元格是否为零。如果是,你应该初始化它

默认方法如下所示:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...

return cell;
于 2013-10-11T09:14:24.630 回答
1
return cell;
return cellRead;

在 C 和所有其他具有 return 语句的语言中,任何方法都将在到达第一个 return 语句时被留下并终止,return cellRead;永远不会到达。

而不是为 2 种不同的布局使用一个自定义单元格,您应该使用 2。

- (void) viewDidLoad {
   [super viewDidLoad];

   [self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"Cell"];
   [self.tableView registerClass:[MyReadCell class] forCellReuseIdentifier:@"CellRead"];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    static NSString *CellIdentifierRead = @"CellRead";

    UITableView *cell;


    notifications *n = [self.GPTNotifications objectAtIndex:indexPath.row];

    if (n.read == false) {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        MyReadCell *cellRead = (MyReadCell *)cell;
        cellRead.readText.text =n.notifMessage;
        cellRead.reDate.text =n.notifDateD;
        cellRead.resub.text = n.notifDateD;

    }
    if (n.read == true) {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierRead forIndexPath:indexPath];
        MyCell *myCell = (MyCell *)cell;
        myCell.notifTitle.text = n.notifTitleD;
        myCell.notifDate.text = n.notifDateD;
        myCell.notifMsg.text = n.notifMessage;
    }

    // Configure the cell...

    return cell;
}
于 2013-10-11T09:12:10.510 回答