2010-05-28 2 views
4

내 tableview에 있습니다. 첫 번째 이미지에서 처음 볼 때 볼 수없는 마지막 셀이 있습니다. 목록을 스크롤하면 두 번째 이미지에서 볼 수 있습니다. price 또는 my detailTextLabel이 오른쪽 정렬을 유지하지 못하는 새 행에 삽입됩니다. 여기 UITableViewCell UITableViewCellStyleValue1을 사용하여 아래쪽 셀의 detailTextLabel에 새 줄을 추가합니다.

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



image 1 http://img706.imageshack.us/img706/4496/iphoneerror1.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]; 
// ..... 

그런 다음 나머지 코드를 그대로 사용할 수 있습니다. 이것은 재사용 된 셀이 올바른 유형인지 확인합니다.

+1

대단히 감사합니다. – slim

2

모든 행에 동일한 셀 식별자를 사용하고 있지만 0 행의 스타일이 다릅니다. 위로 스크롤 할 때 자막 스타일의 셀을 다시 사용할 수 있습니다.

0 번째 행에 다른 셀 식별자를 사용해보십시오. cell 선언을 스위치 외부에두고 각 경우에 dequeueReusableCellWithIdentifier를 이동하십시오.

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

코드에 몇 가지 설명을 추가하고 그 이유를 설명해 주시겠습니까? 여기에 현재 사용자와 게시물을 보는 모든 다음 사용자에게 도움이되는 실질적인 답변이 필요합니다. 이것은 코드를 설명함으로써보다 쉽게 ​​달성됩니다. –

관련 문제