質問

my problem is I want to add custom designed seperator line between my UITableview cells except first cell of table(indexPath.row=0). Following code seems fine when I reload my table first time. However when I scroll down and scroll up it appears custom seperator line on the top of the first cell of table. I printed indexpath.row value and discovered that if I scrolls up first cell of table is reconstructed at indexpath.row=7. Any solution? Thanks for reply :) My code is:

- (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];
}
役に立ちましたか?

解決

That because the table uses a reused cell that was build with the separator line, you can use two CellIdentifier one for your first row, and another for all the rest..

Try something like (didn't test the code but it should work):

- (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];
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top