2013-07-28 1 views
0

Моя проблема заключается в том, что я хочу добавить настраиваемую линию разделителя между моими ячейками UITableview, кроме первой ячейки таблицы (indexPath.row=0). Следующий код кажется прекрасным, когда я перезагружаю таблицу в первый раз. Однако, когда я прокручиваю вниз и прокручиваю вверх, появляется строка пользовательского seperator в верхней части первой ячейки таблицы. Я напечатал значение indexpath.row и обнаружил, что если я прокручиваю первую ячейку таблицы, то восстанавливается на indexpath.row=7. Любое решение? Спасибо за ответ :) Моим код:Проблема с разделителем-разделителем UItableview по адресу indexpath.row = 0

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

static NSString *CellIdentifier = @"CustomCellIdentifier"; 

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = (CustomCell *)[CustomCell cellFromNibNamed:@"CustomTwitterCell"]; 
} 

if(indexPath.row!=0) 
{ 

    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 2.5)]; 

    lineView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"line.png"]]; 

    [cell.contentView addSubview:lineView]; 

    [lineView release]; 
} 

    NSDictionary *tweet; 

    tweet= [twitterTableArray objectAtIndex:indexPath.row]; 

    cell.twitterTextLabel.text=[tweet objectForKey:@"text"]; 
    cell.customSubLabel.text=[NSString stringWithFormat:@"%d",indexpath.row]; 
} 

ответ

1

Это потому, что таблица использует повторно использовать ячейку, которая была построена с разделительной линией, вы можете использовать два CellIdentifier один для первой строки, а другие для всех остальных. .

Попробуйте что-то вроде (не проверял код, но он должен работать):

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

    static NSString *FirstCellIdentifier = @"FirstCellIdentifier"; 
    static NSString *OthersCellIdentifier = @"OthersCellIdentifier"; 

    NSString *cellIndentitier = indexPath.row == 0 ? FirstCellIdentifier : OthersCellIdentifier; 

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentitier]; 
    if (cell == nil) { 
     cell = (CustomCell *)[CustomCell cellFromNibNamed:cellIndentitier]; 

     if(indexPath.row!=0) { 
      UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 2.5)]; 

      lineView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"line.png"]]; 

      [cell.contentView addSubview:lineView]; 

      [lineView release]; 
     } 
    } 

    NSDictionary *tweet; 

    NSDictionary *tweet= [twitterTableArray objectAtIndex:indexPath.row]; 

    cell.twitterTextLabel.text = [tweet objectForKey:@"text"]; 
    cell.customSubLabel.text = [NSString stringWithFormat:@"%d",indexpath.row]; 
} 
+0

Спасибо, это сработало :) – user1951145