UIView出现在多个UITableViewCells上

时间:2013-07-04 01:07:38

标签: iphone uitableview drawing 2d cell

我有一个小社交网络,我试图将评级代码实施到表格视图中,它判断星级评级...问题是第二部分显示第一个表格部分的评级

图片:

enter image description here

所以你可以看到它在一些tableview单元格中显示评级,只是最后一对,为什么会这样做呢?

绘图代码:

 -(void)drawRect:(CGRect)rect
{
if(_shouldDrawRating)
{
    self.desc_image.layer.cornerRadius = 8;
    self.desc_image.clipsToBounds = YES;
    _rating = [[UIStarRateView alloc]initWithFrame:CGRectMake(220,35,70,15)];
    [_rating setMaxStars:5];
    [_rating setFullImage:[UIImage imageNamed:@"StarFull.png"]];
    [_rating setHalfImage:[UIImage imageNamed:@"StarHalf.png"]];
    [_rating setEmptyImage:[UIImage imageNamed:@"StarEmpty.png"]];
    [_rating setRate:_postRating];
    [self addSubview:_rating];
}
}

单元格渲染代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Regular_Cell *cell;
// Configure the cell...
if(indexPath.section == 0)
{
    if(indexPath.row < NUMBER_OF_DYNAMIC_ROWS_RECENT)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"default"];
        if(cell == nil)
        {
            cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"default"];
        }
        NSDictionary *dict = self.recentArray[indexPath.row];
        [cell setShouldDrawRating:YES];
        [cell.title_label setText:dict[@"title"]];;
        [cell.author_label setText:[NSString stringWithFormat:@"Author: %@",dict[@"author"]]];
        [cell.views_label setText:[NSString stringWithFormat:@"Views: %@",dict[@"views"]]];
        [cell setPoll_idFromString:[NSString stringWithFormat:@"%@",dict[@"id"]]];
        //options for image view
        cell.desc_image.contentMode = UIViewContentModeScaleAspectFill;
        NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",dict[@"imageURL"]]];
        [cell.desc_image setImageURL:imageURL];
        [cell setPostRating:indexPath.row+0.5];
    }
    else if(indexPath.row == NUMBER_OF_DYNAMIC_ROWS_RECENT)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"See More"];
        if(cell == nil)
        {
            cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"See More"];
        }
    }
    else
    {
        cell = nil;
    }
    NSLog(@"%@",indexPath);
}
if(indexPath.section == 1)
{
    if(indexPath.row < NUMBER_OF_DYNAMIC_ROWS_TOP)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"default"];
        if(cell == nil)
        {
            cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"default"];
        }
        NSDictionary *dict = self.topViewedArray[indexPath.row];
        [cell.title_label setText:dict[@"title"]];
        [cell.author_label setText:[NSString stringWithFormat:@"Author: %@",dict[@"author"]]];
        [cell.views_label setText:[NSString stringWithFormat:@"Views: %@",dict[@"views"]]];
        [cell setPoll_idFromString:[NSString stringWithFormat:@"%@",dict[@"id"]]];
        //options for image view
        cell.desc_image.contentMode = UIViewContentModeScaleAspectFill;
        NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",dict[@"imageURL"]]];
        [cell.desc_image setImageURL:imageURL];
    }
    else if(indexPath.row == NUMBER_OF_DYNAMIC_ROWS_TOP)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"See More"];
        if(cell == nil)
        {
            cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"See More"];
        }
    }
    else
    {
        cell = nil;
    }
    NSLog(@"%@",indexPath);
}
return cell;
}

2 个答案:

答案 0 :(得分:0)

在第1部分中,你必须将布尔值设置为NO,它现在在第1部分中没有正确的值。我想你错过了这一行。

    [cell setShouldDrawRating:NO];

不仅在第1节中你必须实现它的创建单元格,否则会导致绘图不一致的问题

答案 1 :(得分:0)

替换用于设置tableView:cellForRowAtIndexPath:的单元格标签,图片等的代码,并将其粘贴到tableView:willDisplayCell:forRowAtIndexPath:中。每个部分都有setShouldDrawRating:,不仅仅是0号部分。