0

我的问题是我想在我的 UITableview 单元格之间添加自定义设计的分隔线,除了 table( indexPath.row=0) 的第一个单元格。当我第一次重新加载我的表时,以下代码似乎很好。但是,当我向下滚动并向上滚动时,它会在表格的第一个单元格的顶部出现自定义分隔线。我打印indexpath.row了 value 并发现如果我向上滚动表格的第一个单元格,则会在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];
}
4

1 回答 1

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];
}
于 2013-07-28T11:52:32.300 回答