【问题标题】:UItableView deplicated cell when scrolling滚动时UItableView deplicated单元格
【发布时间】:2015-07-16 13:28:07
【问题描述】:

很抱歉再次发布此问题,但我查看了很多答案,但都没有帮助解决我的问题。

这是我的代码:

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

static NSString *cellIdentifier = @"radioCell";

RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[RadioTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}

[self configureCommentCell:cell atIndexPath:indexPath];



return cell;
}

当我向下滚动时,我的单元格被混淆并且一些数据重复,所以我尝试了这个:

static NSString *CellIdentifier = @"memberCell";
RadioCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
        cell = [[RadioTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

还有这个:

    RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
        cell = [[RadioTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    }

但它并没有解决我的问题,我得到白色的空单元格?请问这个问题怎么解决?

更新

- (void)configureCommentCell:(RadioTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSDictionary *object;
if ([_dataArray[indexPath.section] isKindOfClass:[NSArray class]])
    object = [_dataArray[indexPath.section] objectAtIndex:indexPath.row];
else
    object = [[_dataArray[indexPath.section] valueForKey:@"radioList"] objectAtIndex:indexPath.row];

if (object[@"jsonUrl"]) {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:object[@"jsonUrl"] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //NSDictionary *tempObject = (NSDictionary *) responseObject;
        if (![[responseObject objectForKey:@"type"] isEqualToString:@"error"]) {
            NSDictionary *tempObject = [responseObject[@"data"] objectAtIndex:0];
            cell.playingNow.text = tempObject[@"song"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

cell.name.text = [NSString stringWithFormat:@"  %@", object[@"title"]];
if (object[@"logoUrl"])
    [cell.logo setImageWithURL:[NSURL URLWithString:object[@"logoUrl"]]];
}

【问题讨论】:

  • 能否请您添加您的方法 cellforrow..?
  • @AbhishekSharma 是的,检查我的更新
  • @Leo 检查我的更新
  • @AbhishekSharma 这里有什么更新吗?

标签: ios objective-c uitableview cocoa-touch uikit


【解决方案1】:

我看到您的问题是您正在获取 configureCommentCell 内的单元格数据,这在 cellForRowAtIndexPath 内被调用。这是错误的,因为在cellForRowAtIndexPath 中获取数据为时已晚,在此委托方法中您应该返回单元格。

可以在从服务器检索数据之前调用此行:

cell.name.text = [NSString stringWithFormat:@"  %@", object[@"title"]];

您应该:

  1. 在单独的方法中获取数据,例如fetchData

  2. 当数据在AFNetworking 方法的完成块中下载时,将数据存储在NSArray 中,例如myDataArray 仍然在完成块调用[self.tableView reloadData];

  3. 在 viewDidLoad 方法中只需调用你的方法fetchData

    你的cellForRowAtIndexPath 应该是这样的:

     -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
          // hey please give me the cell to display ... harry up please
          // please harry up ! oh my god  you are fetching data from server 
          // while I am asking for the cell ! 
          // ok I don't care do what you want 
          // I will return an empty cell anyway
          // and guess what I will not take in consideration
          // the retried data  because it's inside a block 
          // which is called asynchronously
    
    
          static NSString *cellIdentifier = @"radioCell";
    
          RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];    if (cell == nil) {
           cell = [[RadioTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];    }
    
     // now before return the cell you need to update the content of cell    
     // maybe you have an array of items and you should update the label        
     // for example here and then return the cell
    
        cell.usernameLabel = self.myDataArray[indexPath.row]; // example
    
    
        return cell; 
    }
    

【讨论】:

  • 我已经尝试过了,它给出了相同的结果空白单元格
  • 不,这是错误的,配置单元对代码没有影响,这只是让您的代码更加干净的方法。将代码从 configureCell 移动到 cellForRow 也不会改变结果。
  • @Chlebta 你读过我的回答吗?实际上我没有告诉您将代码移动到cellForRowAtIndexPath 实际上您的问题是您正在该方法中获取数据,请再次阅读我的答案,我确定问题的原因
  • @Chlebta 在我使用的所有项目中 AFNetworking 。顺便阅读我更新的答案(代码中的 cmets)
【解决方案2】:

好吧,TableView 正在重用单元格,并且每次显示单元格时都添加图像。因此,当重用单元格时,您添加了另一个图像,但已经有一个图像。

您将不得不重复使用图像视图,并且只有在创建单元格时才添加图像。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifer = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer]autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   


        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20,0,30,44)];
        imageView.tag = 1001;
        [cell addSubview:imageView];
        [imageView release], imageView= nil;
    }

    TabBarTestAppDelegate *delegate = (TabBarTestAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSArray *local = delegate.myData;
    // ok, it's horrible, don't look at it   :-)
    cell.textLabel.text = [NSString stringWithFormat:@"%@%@", @"       " ,[local objectAtIndex:indexPath.row]];
    //

    NSString* name = nil;;
    if (indexPath.row == 0) {
        name = @"topicon";
    }
    else if (indexPath.row + 1 == [local count]) {
        name = @"bottomicon";
    }
    else {
        name = @"innericon";
    }

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1001];
    imageView.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:name ofType:@"png"]];

    return cell;
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多