繁体   English   中英

滚动时的UItableView复制单元格

[英]UItableView deplicated cell when scrolling

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

所以这是我的代码:

-(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"]]];
}

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

在从服务器检索数据之前,可能会调用以下行:

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

相反,您应该:

  1. 在单独的方法中获取数据,例如fetchData
  2. 当在AFNetworking方法的完成块中下载数据时,将数据存储在名为myDataArrayNSArray ,仍在完成块调用[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; } 

好了,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;
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM