0

我创建了一个自定义UITableViewCell,每个单元格都AVPlayerUITableViewController. 在每个单元格上,我都有一个播放按钮、暂停按钮和加载指示器。

当我播放音频时,元素按要求工作,并在播放器状态发生变化时发生变化,例如播放时,暂停按钮出现,播放按钮消失。当我在第二个单元格上播放音频时,第一个单元格知道这一点,将其重置为按钮状态,然后第二个单元格执行它的业务。

所以这个功能完美地工作,唯一的问题是因为UITableViewCells 被重复使用,当我向下滚动到下面的单元格时,我开始看到暂停按钮出现在它们上面。这是因为它们与上面的单元格相同(重用),并且因为我的单元格是我的自定义子类的代表AVPlayer,所以音频播放器正在向一个不正确的单元格发送消息。

我该怎么做才能使每个UITableViewCell单独的委托对象为我的AVPlayer

4

3 回答 3

1

您必须在重用时从单元格中删除元素:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} else {
    /* prepare for reuse */
    [cell.playButton removeFromSuperview];
    /* or */
    [[cell viewWithTag:10] removeFromSuperview];
}
于 2012-09-02T16:26:11.687 回答
0

我从 JSON blob 加载的图像也有同样的问题。我使用 GCD 并将图像保存到与分配给每个单元格的键配对的 NSDictionary 中。

- (UIImage *)imageForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    // get the dictionary for the indexPath
    NSDictionary *tweet = [tweets objectAtIndex:[indexPath row]];

    // get the user dictionary for the indexPath
    NSDictionary *user = [tweet objectForKey:@"posts"];

    //get the image URL
    NSURL *url = [NSURL URLWithString:[[[[[tweet objectForKey:@"photos"] objectAtIndex:0] objectForKey:@"alt_sizes"] objectAtIndex:3] objectForKey:@"url"]];

    // get the user's id and check for a cached image first
    NSString *userID = [user objectForKey:@"id"];
    UIImage *image = [self.images objectForKey:userID];
    if(!image)
    {

        // if we didn't find an image, create a placeholder image and
        // put it in the "cache". Start the download of the actual image
        image = [UIImage imageNamed:@"Placeholder.png"];
        [self.images setValue:image forKey:userID];

        //get the string version of the URL for the image
        //NSString *url = [user objectForKey:@"profile_image_url"];

        // create the queue if it doesn't exist
        if (!queue) {
            queue = dispatch_queue_create("image_queue", NULL);
        }

        //dispatch_async to get the image data
        dispatch_async(queue, ^{

            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *anImage = [UIImage imageWithData:data];
            [self.images setValue:anImage forKey:userID];
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

            //dispatch_async on the main queue to update the UI
            dispatch_async(dispatch_get_main_queue(), ^{
                cell.imageView.image = anImage;
            });
        });


    }

    // return the image, it could be the placeholder, or an image from the cache
    return image;
}
于 2012-09-02T15:33:37.193 回答
-1

UITableViewController我通过为音频播放器设置代理解决了这个问题。然后将“当前播放”单元格保存indexPath@property.UITableViewController

然后在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath我检查 indexPath 是否与“当前播放”单元格的相同indexPath,如果是,则设置“音频播放”按钮排列,如果不是,则设置默认按钮排列。

这在区分单元格时效果更好,因为您有一个唯一的标识符indexPath来比较它们。

于 2012-09-02T16:48:02.747 回答