0

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

图片:

在此处输入图像描述

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

绘图代码:

 -(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;
}
4

2 回答 2

0

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

    [cell setShouldDrawRating:NO];

不仅在第 1 节中创建单元格时必须执行,否则会导致绘图不一致的问题

于 2013-07-04T06:17:59.730 回答
0

替换用于设置单元格标签、图像等的代码tableView:cellForRowAtIndexPath:并将其粘贴到tableView:willDisplayCell:forRowAtIndexPath:. 并且setShouldDrawRating:在每个部分中,不仅在第 0 部分中。

于 2013-07-04T13:59:51.600 回答