2013-08-23 47 views
1

在我使用下面的代码段中,细节文本标签不显示:detailtext标签没有显示出来

-(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的正确方法?

回答

2

根据此SO post,注册UITableViewCell意味着所有单元格将以默认样式实例化。副标题和右侧和左侧的细节单元不适用于registerClass:forCellReuseIdentifier:

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; 

}

希望这对你有所帮助!

-1

在第二种方法中,您不是将细胞排队,而是实际创建一个新细胞。这是不可取的。相反,使用1号的方法,但更换行:

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

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

这是因为它包括indexPath方法将总是返回你的细胞,所以检查;

if(!cell) 

将始终返回true,因此您将无法使用其他样式创建单元格。但是使用没有索引路径的方法将返回nil,如果单元格之前未创建...您可以阅读Apple提供的UITableViewCell文档的更多内容:)