2013-07-28 102 views
0

自定义设计的分隔符问题我的问题是我想在我的UITableview单元格(indexPath.row=0)的第一个单元格之外添加自定义设计的分隔符行。下面的代码看起来很好,当我第一次重新加载我的表。但是,当我向下滚动并向上滚动时,它会在表的第一个单元格的顶部出现自定义分隔线。我打印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