数据与所选单元格一起显示在其他uitableviewcells中

时间:2014-10-17 06:37:14

标签: ios uitableview

我是iphone开发的新手。我有一个带有自定义UITableViewCell的UITableView。每个单元格都包含动态创建的标签,按钮。 我的功能是我必须在单击按钮时显示视图并使用相同的按钮隐藏它。我可以在点击按钮时显示视图。问题是它也出现在其他细胞中。并且在滚动表格时,它也会出现在随机单元格中。 我维护了一个数组来标记所选单元格的位置,并根据该数组显示视图。数组中的数据是正确的。但它仍然出现在其他细胞中。 请帮我解决这个问题。 先谢谢你。

以下是我的CellforRowatIndexPath代码,以及按钮上的函数点击

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
int y=0;

childCell *cell= [tableView dequeueReusableCellWithIdentifier:nil];

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

if (cell == nil)
{
    rateImage = [[UIButton alloc]init];
    Rate =[[UIButton alloc]init];

    NSString *ratingCount;
    cell = [[childCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    [rateImage setBackgroundImage:[UIImage imageNamed:@"bluestar1.png"] forState:UIControlStateNormal];
    rateImage.frame= CGRectMake(240, y, 15, 15);
    rateImage.tag = indexPath.row ;
    [rateImage addTarget:self action:@selector(Rate_Commentchild:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:rateImage];

    //RATE COUNT

    if(![ratingCount isEqualToString:@"0"])
    {
        [Rate setTitle:[@"Rate" stringByAppendingFormat:@"(%@)", ratingCount] forState:UIControlStateNormal];
        [Rate sizeToFit];
    }
    else{
        [Rate setTitle:@"Rate" forState:UIControlStateNormal];

    }
    Rate.tag = indexPath.row ;
    [Rate setFont:[UIFont systemFontOfSize:13.0]];
    [Rate addTarget:self action:@selector(Rate_Commentchild:) forControlEvents:UIControlEventTouchUpInside];
    Rate.frame = CGRectMake(250, y-2, 50, 20);
    [Rate setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cell addSubview:Rate];

    cv = [[ASStarRatingView alloc]initWithFrame:CGRectMake(190, y+10, 140, 30)]; //creat an instance of your custom view
                                                                                 //[cv setBackgroundColor:[UIColor lightGrayColor]];
    cv.tag = indexPath.row ;
    if([delegate.constants.snsSubCommentrate[indexPath.row] isEqualToString: @"0"])
    {
        cv.hidden=TRUE;
    }
    else
    {
        cv.hidden=FALSE;
    }
    [cell addSubview:cv];

}
return cell;}

-(void)Rate_Commentchild:(UIButton *)sender
{
    NSLog(@"entered rate function");

    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UIButton *b = (UIButton *)sender;
    NSInteger row = b.tag ;

    UITableViewCell *buttonCell = (UITableViewCell *)[b superview];
    UITableView* table = (UITableView *)[buttonCell superview];
    NSIndexPath* pathOfTheCell = [table indexPathForCell:buttonCell];

    NSLog(@"path of cell  %@",pathOfTheCell);
    NSLog(@"stars array  %@",delegate.constants.snsSubCommentrate);

    BOOL isRatingAlreadyVisible = FALSE;
    int RatingVisibleAtPosition = 1;

    for (int i=0; i<[delegate.constants.snsSubCommentrate count]; i++) {
        if([[delegate.constants.snsSubCommentrate objectAtIndex:i] isEqualToString:@"1"])
        {
            isRatingAlreadyVisible = TRUE;
            RatingVisibleAtPosition = i;
        }
    }

    if(isRatingAlreadyVisible == TRUE && row != RatingVisibleAtPosition)
    {   delegate.constants.snsSubCommentrate[RatingVisibleAtPosition] = @"0";
        [subCommentsTable reloadData];
    }


    if(![delegate.constants.user_id isEqualToString:[delegate.constants.snsSubCommentCreatedBy objectAtIndex:row]])
    {
        NSLog(@"Can rate this comment");
        //stars are hidden
        if([delegate.constants.snsSubCommentrate[row] isEqualToString:@"0"])
        {
            delegate.constants.snsSubCommentrate[row]=@"1";
            rating_layout.hidden=FALSE;
        }

        //stars are visible
        else{
            delegate.constants.snsSubCommentrate[row]=@"0";
            rating_layout.hidden=TRUE;
        }
        [subCommentsTable reloadRowsAtIndexPaths:@[pathOfTheCell] withRowAnimation:UITableViewRowAnimationLeft];
        [subCommentsTable reloadInputViews];
    }
    else
        NSLog(@"Can't rate this comment"); 
    NSLog(@"stars array  %@",delegate.constants.snsSubCommentrate);
}

0 个答案:

没有答案