2010-05-28 38 views
4

在我的桌面视图我有最后一个单元格,最初不可见如第一个图像中看到的,当我向上滚动列表时,您可以看到第二个图像价格或我的detailTextLabel被放在一个不保持正确理由的新线上。
image 1 http://img706.imageshack.us/img706/4496/iphoneerror1.jpgUITableViewCell与UITableViewCellStyleValue1,添加新线到detailTextLabel在底部的单元格

image 2 http://img706.imageshack.us/img706/6007/iphoneerror2edited.jpg


下面是代码,我想不通为什么它这样做,任何方向或帮助将不胜感激

- (UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [ltableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    // Configure the cell. 
    NSUInteger indexRow = [indexPath row]; 
    switch (indexRow) { 
     case 0:{ 
      NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet]; 
      NSString *description = [[currentData objectForKey:@"Description"] stringByTrimmingCharactersInSet:set]; 

      if (cell == nil) { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
      } 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      cell.accessoryType = UITableViewCellAccessoryNone; 
      cellShift = 1; 


      if (![description isEqualToString:@""]) { 
       cell.textLabel.text = @""; 
       cell.detailTextLabel.text = description; 
       cell.detailTextLabel.numberOfLines = 2; 
      } 
      else { 
       cell.textLabel.text = @""; 
       cell.detailTextLabel.text = @""; 
       cell.detailTextLabel.numberOfLines = 0; 


      } 
      break; 
     } 
     default:{ 
      if (cell == nil) { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
      } 
      NSDictionary *item = [tableData objectAtIndex:(indexRow-cellShift)]; 
      NSString *name = [item objectForKey:@"Name"]; 
      if ([name length] > MaxVendorsLength) { 
       name = [NSString stringWithFormat:@"%@ ...",[name substringToIndex:MaxVendorsLength]]; 
      } 
      cell.textLabel.text = name; 
      cell.textLabel.minimumFontSize = 12; 

      NSString *priceString; 
      float price = [[item objectForKey:@"Price"] floatValue]; 
      //NSLog(@"| %@ | : | %@ |",[item objectForKey:@"Name"], [item objectForKey:@"Price"]); 
      if (price != 0) { 
       priceString = [[NSString alloc] initWithFormat:@"$%.2f",price]; 
      } 
      else { 
       priceString = [[NSString alloc] initWithString:@"--"]; 
      } 

      cell.detailTextLabel.text = priceString; 

      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
      [priceString release];   
      break; 
     } 
    } 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 
    cell.textLabel.minimumFontSize = 14; 
    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:15]; 
    cell.detailTextLabel.minimumFontSize = 14; 
    return cell; 
} 

让我知道如果我需要发布其他任何东西来获得这个帮助?

回答

9

这是因为当您向上滚动时,顶部单元格被重用,因为您的所有单元格都具有相同的单元格标识符(第一行)。您需要声明两个单元格标识符,并根据您尝试获取的行来使用适当的单元格标识符。

static NSString *FirstRowCellIdentifier = @"A"; 
static NSString *OtherRowCellIdentifier = @"B"; 

NSString *cellIdentifier = nil; 
if ([indexPath row] == 0) 
    cellIdentifier = FirstRowCellIdentifier; 
else 
    cellIdentifer = OtherRowCellIdentifier; 

UITableViewCell *cell = [ltableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
// ..... 

然后,您可以按原样使用其余代码。这只是确保resused单元格是正确的类型。

+1

真棒,工作很好,非常感谢 – slim 2010-05-28 23:58:13

2

您对所有行使用相同的单元格标识符,但第0行的样式不同。滚动时,字幕样式的单元格可能会重新使用。

尝试对第0行使用不同的单元格标识符。只保留开关外部cell的声明并将dequeueReusableCellWithIdentifier移动到每个案例。

1
(UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
+0

您可以添加一些解释你的代码解释你做了什么,为什么?在此,我们希望有实质性的答案对当前用户和下一个看到你的帖子的人有帮助;这通过解释代码更容易实现。 – 2014-02-11 07:50:19

相关问题