1

当我使用下面的代码段时,详细文本标签不显示:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"NEW";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];

    UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath ];
    if(cell==nil)
    {     
    cell =  [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    }
    NSDictionary* item = [saleItems objectAtIndex:[indexPath row]];
    cell.textLabel.text = [item valueForKey:@"name"];
    cell.detailTextLabel.text =  [item valueForKey:@"store"];

    return cell;




}

但是,当我将上述方法修改为以下详细信息时,出现了:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"NEW";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];


    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    NSDictionary* item = [saleItems objectAtIndex:[indexPath row]];
    cell.textLabel.text = [item valueForKey:@"name"];
    cell.detailTextLabel.text =  [item valueForKey:@"store"];

    return cell;


}

第一种方法出了什么问题?使用 dequeueReusableCellWithIdentifier 的正确方法是什么?

4

3 回答 3

2

根据这个SO post,注册UITableViewCell意味着所有单元格都将使用默认样式进行实例化。字幕和左右细节单元格不适用于registerClass:forCellReuseIdentifier:.

于 2013-08-23T02:47:19.257 回答
2

因为您创建了默认样式。您问题中的某些方法可从 iOS 6 获得。您确定要针对 iOS 6

您可以尝试此示例代码(不仅适用于 ios 6):

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"NEW";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    // if you sure the cell is not nil (created in storyboard or everywhere) you can remove "if (cell == nil) {...}"
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    NSDictionary* item = [saleItems objectAtIndex:[indexPath row]];
    cell.textLabel.text = [item valueForKey:@"name"];
    cell.detailTextLabel.text =  [item valueForKey:@"store"];

    return cell;

}

希望这对你有帮助!

于 2013-08-23T02:59:27.913 回答
-1

In the second method, you are not de-queue-ing the cell but actually creating a new cell. This is not advisable. Instead, use the 1st method, but replace the line:

UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath ];

with

UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];

This is because the method which includes the indexPath will always return you the cell, so the check;

if(!cell)

will always return true, and hence you will not able to create the cell with another style. But using the method without the index path will return nil, if the cell was not created before... You can read more on the UITableViewCell docs provided by Apple :)

于 2014-04-04T03:33:06.460 回答