在UITextView parse.com中显示时,NSString值会覆盖其他值

时间:2015-07-13 15:53:28

标签: ios objective-c xcode uitableview parse-platform

如果我将titleTextView.backgroundColor = [UIColor clearColor]更改为任何其他颜色,它会显示我需要的颜色,但如果我保持原样,则覆盖所有[object objectForKey:@" imageTitle" ]值。

有没有人遇到过这样的问题?有什么建议吗?

这就是我现在正在做的事情。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";

NSUInteger index = [self indexForObjectAtIndexPath:indexPath];

if (indexPath.row % 2 == 0) {
    // Header
    return [self detailPhotoCellForRowAtIndexPath:indexPath];
} else {
    // Photo
    PhotoViewCell *cell = (PhotoViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[PhotoViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    cell.photoButton.tag = index;
    cell.imageView.image = [UIImage imageNamed:@"PlaceholderPhoto.png"];

    if (object) {


        UITextView * titleTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, cell.imageView.bounds.size.width/2, cell.imageView.bounds.size.width, cell.imageView.bounds.size.width)];

        cell.imageView.file = [object objectForKey:kPAPPhotoPictureKey];



        titleTextView.backgroundColor = [UIColor clearColor];
        titleTextView.text = [object objectForKey:@"imageTitle"];
        titleTextView.font = [UIFont fontWithName:@"Arial" size:30];
        titleTextView.textColor = [UIColor redColor];
        [cell.contentView addSubview: titleTextView];


        static NSString *CellIdentifier = @"footerCell";

        PhotoFooterView *footerView = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (!footerView) {
            footerView = [[PhotoFooterView alloc] initWithFrame:CGRectMake( 0.0f, cell.imageView.frame.size.height, self.view.bounds.size.width, 44.0f) buttons:PhotoFooterButtonsDefault];
            footerView.delegate = self;




        }
        PFObject *object = [self objectAtIndexPath:indexPath];
        footerView.photo = object;
        footerView.tag = index;
        [footerView.likeButton setTag:index];

        NSDictionary *attributesForPhoto = [[APPCache sharedCache] attributesForPhoto:object];

        if (attributesForPhoto) {
            [footerView setLikeStatus:[[APPCache sharedCache] isPhotoLikedByCurrentUser:object]];
            [footerView.likeButton setTitle:[[[APPCache sharedCache] likeCountForPhoto:object] description] forState:UIControlStateNormal];
            [footerView.commentButton setTitle:[[[APPCache sharedCache] commentCountForPhoto:object] description] forState:UIControlStateNormal];

            if (footerView.likeButton.alpha < 1.0f || footerView.commentButton.alpha < 1.0f) {
                [UIView animateWithDuration:0.200f animations:^{
                    footerView.likeButton.alpha = 1.0f;
                    footerView.commentButton.alpha = 1.0f;
                }];
            }
        } else {
            footerView.likeButton.alpha = 0.0f;
            footerView.commentButton.alpha = 0.0f;

            @synchronized(self) {
                // check if we can update the cache
                NSNumber *outstandingSectionHeaderQueryStatus = [self.outstandingSectionHeaderQueries objectForKey:@(index)];
                if (!outstandingSectionHeaderQueryStatus) {
                    PFQuery *query = [AAPUtility queryForActivitiesOnPhoto:object cachePolicy:kPFCachePolicyNetworkOnly];
                    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                        @synchronized(self) {
                            [self.outstandingSectionHeaderQueries removeObjectForKey:@(index)];

                            if (error) {
                                return;
                            }

                            NSMutableArray *likers = [NSMutableArray array];
                            NSMutableArray *commenters = [NSMutableArray array];

                            BOOL isLikedByCurrentUser = NO;

                            for (PFObject *activity in objects) {
                                if ([[activity objectForKey:kPAPActivityTypeKey] isEqualToString:kPAPActivityTypeLike] && [activity objectForKey:kPAPActivityFromUserKey]) {
                                    [likers addObject:[activity objectForKey:kPAPActivityFromUserKey]];
                                } else if ([[activity objectForKey:kPAPActivityTypeKey] isEqualToString:kPAPActivityTypeComment] && [activity objectForKey:kPAPActivityFromUserKey]) {
                                    [commenters addObject:[activity objectForKey:kPAPActivityFromUserKey]];
                                }

                                if ([[[activity objectForKey:kPAPActivityFromUserKey] objectId] isEqualToString:[[PFUser currentUser] objectId]]) {
                                    if ([[activity objectForKey:kPAPActivityTypeKey] isEqualToString:kPAPActivityTypeLike]) {
                                        isLikedByCurrentUser = YES;
                                    }
                                }
                            }

                            [[APPCache sharedCache] setAttributesForPhoto:object likers:likers commenters:commenters likedByCurrentUser:isLikedByCurrentUser];

                            if (footerView.tag != index) {
                                return;
                            }

                            [footerView setLikeStatus:[[APPCache sharedCache] isPhotoLikedByCurrentUser:object]];
                            [footerView.likeButton setTitle:[[[APPCache sharedCache] likeCountForPhoto:object] description] forState:UIControlStateNormal];
                            [footerView.commentButton setTitle:[[[APPCache sharedCache] commentCountForPhoto:object] description] forState:UIControlStateNormal];

                            if (footerView.likeButton.alpha < 1.0f || footerView.commentButton.alpha < 1.0f) {
                                [UIView animateWithDuration:0.200f animations:^{
                                    footerView.likeButton.alpha = 1.0f;
                                    footerView.commentButton.alpha = 1.0f;
                                }];
                            }
                        }
                    }];
                }
            }
        }

        [cell addSubview:footerView];

        // PFQTVC will take care of asynchronously downloading files, but will only load them when the tableview is not moving. If the data is there, let's load it right away.
        if ([cell.imageView.file isDataAvailable]) {
            [cell.imageView loadInBackground];
        }
      }
    return cell;
    }
}

提前致谢

1 个答案:

答案 0 :(得分:0)

问题是UITableView单元格被重用。当一个单元格从表格的边缘滚动时,&#34; new&#34;在另一侧取代它的单元格实际上是同一个对象(在视图创建和销毁方面可以节省大量成本)。

dequeueReusableCellWithIdentifier:返回其中一个单元格,大部分时间从滚动位置重复使用。您的cellForRowAtIndexPath每次都会添加一个文本视图 ,这意味着大多数情况下它都是在文本视图中堆叠文本视图。清晰的颜色背景让您注意到错误。

修复是有条件地构建文本视图,只有当单元格还没有,就像这样......

    // your existing code up to here

    UITextView *titleTextView = (UITextView *)[cell viewWithTag:99];
    // this condition guards against building another text view when you have one already
    if (!textView) {
        titleTextView = [[UITextView alloc]initWithFrame: // and so on
        // so we can find it later in the viewWithTag line above
        titleTextView.tag = 99;
        // your code to build the text view here, but not the line
        // that sets the text.  we do that unconditionally for every cell
        [cell.contentView addSubview: titleTextView];
    }
    // this part of the view varies by cell, so do it unconditionally...
    titleTextView.text = [object objectForKey:@"imageTitle"];

    // your existing code from here

顺便说一句,你cellForRowAtIndexPath方法的其余部分是漫长且非标准的,两个警告标志着前方的麻烦。第二次打电话给#34; footerView&#34;我特别感到困惑。

在没有先检查您是否已经获取数据的情况下,在此方法中启动异步调用绝对是不明智的。请记住,当用户滚动相同的单元格时,会快速调用此方法。出于同样的原因,您只需为每个单元格构建一个文本视图,您需要一个防护来确保每个单元格的单个请求(或更少)。

解决这个问题的第一步是将所有与模型相关的代码移出数据源方法。让模型缓存结果,如果已经获取结果,立即返回完成。