UItableview自定义设计的分隔符问题在indexpath.row = 0

时间:2013-07-28 10:24:30

标签: uitableview custom-cell

我的问题是我想在我的UITableview单元格之间添加自定义设计的分隔线,除了表格(indexPath.row=0)的第一个单元格。当我第一次重新加载表时,以下代码似乎很好。然而,当我向下滚动并向上滚动时,它会在表格的第一个单元格的顶部显示自定义分隔线。我打印了indexpath.row值,发现如果我向上滚动,表格的第一个单元格会在indexpath.row=7重建。有解决方案吗谢谢回复:)我的代码是:

- (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 个答案:

答案 0 :(得分: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];
}