当我在UITableView中向下滚动时,单元格消失了。

3
当我在tableView中向下滚动时,一些单元格的内容(标签和图像视图)会消失。 我的代码:
-(void)viewWillAppear:(BOOL)animated{
    [comentarios removeAllObjects];

    NSString *lookup=[NSString stringWithFormat:@"http://my.url"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:lookup]];
    [request setHTTPMethod:@"GET"];
    NSError *error = nil; NSURLResponse *response = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSMutableArray *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSLog(@"%@",jsonDict);

    for (int i=0; i<[jsonDict count]; i++) {
        Comentario *come=[[Comentario alloc] init];
        come.nick=[[jsonDict objectAtIndex:i] objectForKey:@"nick"];
        come.comment=[[jsonDict objectAtIndex:i] objectForKey:@"content"];
        come.avatar=[[jsonDict objectAtIndex:i] objectForKey:@"color"];
        [comentarios addObject:come];
    }
    [self reloadInputViews];
    [self.comentariosTableView reloadData];
}

并且

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if( cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
    }

    // Display recipe in the table cell
    UIImageView *avatar = (UIImageView *)[cell viewWithTag:100];
    avatar.image = [UIImage imageNamed:[[comentarios objectAtIndex:indexPath.row] avatar]];

    UILabel *nick = (UILabel *)[cell viewWithTag:101];
    nick.text =[[comentarios objectAtIndex:indexPath.row] nick];

    UILabel *comment = (UILabel *)[cell viewWithTag:102];
    comment.text = [[comentarios objectAtIndex:indexPath.row] comment];

    UIButton *sinvoto = (UIButton *)[cell viewWithTag:103];

    UIButton *ticket = (UIButton *)[cell viewWithTag:104];

    return cell;
}

我看不出错误,求帮助。提前感谢。

修改 Nª1 刚刚更改了这个

ViewController.m

#import "ViewController.h"
#import "SimpleTableCell.h"

@interface ViewController (){
    NSMutableArray *comentarios;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.comenatrioTableView.delegate=self;
    self.comenatrioTableView.dataSource=self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIImage *plus=[[UIImage imageNamed:@"megafono.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:plus style:UIBarButtonItemStylePlain target:self action:@selector(comenta:)];
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:204.0/255.0 green:0.0/255.0 blue:00.0/255.0 alpha:1.0f];
    comentarios=[[NSMutableArray alloc] init];
    [self reloadInputViews];
}

-(void)viewWillAppear:(BOOL)animated{
    [comentarios removeAllObjects];
    NSString *lookup=[NSString stringWithFormat:@"http://myURL"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:lookup]];
    [request setHTTPMethod:@"GET"];
    NSError *error = nil; NSURLResponse *response = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSMutableArray *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSLog(@"%@",jsonDict);

    for (int i=0; i<[jsonDict count]; i++) {
        Comentario *come=[[Comentario alloc] init];
        come.nick=[[jsonDict objectAtIndex:i] objectForKey:@"nick"];
        come.comment=[[jsonDict objectAtIndex:i] objectForKey:@"content"];
        come.avatar=[[jsonDict objectAtIndex:i] objectForKey:@"color"];
        [comentarios addObject:come];
    }
    [self reloadInputViews];
}

-(void)viewDidAppear:(BOOL)animated{

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [comentarios count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";
    SimpleTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
    Comentario *comentario=[[Comentario alloc] init];
    comentario =[comentarios objectAtIndex:indexPath.row];

    cell.avatar.image=[UIImage imageNamed:[comentario avatar]];
    cell.nick.text=[comentario nick];
    cell.comment.text =[comentario comment];
    return cell;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

-(void)comenta:(id)sender{
    [self performSegueWithIdentifier:@"goComment" sender:self];
}

@end

和 ViewController.h

#import <UIKit/UIKit.h>
#import "Comentario.h"

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *comenatrioTableView;

@end

第三次编辑 问题在于当我向下滚动时,单元格的信息变成了nil,但是评论数组仍有信息。

第四次编辑 这是项目https://github.com/QuimeraKoke/BANG-


也许你的“评论”中的昵称和头像是空的。 - Bannings
你检查了我的答案吗?我在等你的回复。 - Nischal Hada
是的,事实上在滚动向下变为nil之前 - QuimeraKoke
你能同时发布网址吗,这样我就可以查看它了。 - Nischal Hada
我会在我的GitHub上发布该项目的URL。 - QuimeraKoke
2个回答

2

我有几个建议,可以改进你的代码。

viewDidAppearviewWillAppear方法中,你需要调用super:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
}

不要使用:

Comentario *comentario = [[Comentario alloc] init];
comentario = [comentarios objectAtIndex:indexPath.row];

使用:

Comentario *comentario = [comentarios objectAtIndex:indexPath.row];

最后,您应该检查您的数据源:

for (int i=0; i<[jsonDict count]; i++) {
    Comentario *come = [[Comentario alloc] init];
    come.nick = [[jsonDict objectAtIndex:i] objectForKey:@"nick"];
    come.comment = [[jsonDict objectAtIndex:i] objectForKey:@"content"];
    come.avatar = [[jsonDict objectAtIndex:i] objectForKey:@"color"];
    [comentarios addObject:come];

    NSLog(@"nick = %@, comment = %@, avatar = %@", come.nick, come.comment, come.avatar);
}

编辑:

不要使用以下方法:

@interface Comentario : NSObject

@property (weak,nonatomic) NSString *nick;
@property (weak,nonatomic) NSString *comment;
@property (weak,nonatomic) NSString *avatar;

@end

你应该使用:

@interface Comentario : NSObject

@property (copy,nonatomic) NSString *nick;
@property (copy,nonatomic) NSString *comment;
@property (copy,nonatomic) NSString *avatar;

@en

你的问题已经解决。

Copy

当对象是可变的时,需要复制。如果你需要对象的值保持不变,而且你不希望该值受到其他对象所有者所做更改的影响,那么就使用它。因为你在保留副本,所以在完成使用后需要释放对象。

Weak

weak与strong类似,但它不会将引用计数加1。它不是该对象的拥有者,只是持有对它的引用。即使你仍然指向该对象,如果该对象的引用计数降至0,它也将从内存中被释放。

这是一个了解iOS 5 strong和weak的好网站。 http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1

除了上述问题之外,SimpleTableCell的约束也不正确:
enter image description here

你应该去Main.storyboard检查它。(在Interface Builder中选择紧凑宽度和紧凑高度大小类)


1
你正在使用的tableView:cellForRowAtIndexPath:代码相当陈旧。
我建议创建一个自定义的UITableViewCell类,其中包含标签、图像和按钮的属性。
@interface MyTableViewCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *nick;
@property (nonatomic, weak) IBOutlet UILabel *comment;
@property (nonatomic, weak) IBOutlet UIImageView *avatar;
@property (nonatomic, weak) IBOutlet UIButton *sinvoto;
@property (nonatomic, weak) IBOutlet UIButton *ticket;
@end

在你的故事板中,将该单元格的类设置为你的自定义tableViewCell,并将其IBOutlets连接到故事板单元格的标签、图像和按钮。这样就不必使用标记了。
dequeueReusableCellWithIdentifier:调用更改为:
MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

这将始终返回一个单元格,因此您永远不必检查是否为nil。
现在,您可以直接设置单元格的属性:
cell.avatar.image = [UIImage imageNamed:[[comentarios objectAtIndex:indexPath.row] avatar]];
cell.nick.text =[[comentarios objectAtIndex:indexPath.row] nick];
cell.comment.text = [[comentarios objectAtIndex:indexPath.row] comment];

更新:

这一行(与更改键盘有关)是不必要的,可以删除:

[self reloadInputViews];

你为什么要使用一个带有添加的tableView的UIViewController,而不是直接使用UITableViewController呢?

UITableViewController知道如何调整其插入以考虑顶部和底部栏(并且您将设置其automaticallyAdjustsScrollViewInsetsYES)。

在进行 Banning建议的更改后,您可能没问题了。我看不出滚动后单元格为空的其他原因。

如果仍然发生,请发布您的Comentario类,以便我们查看该代码是否影响存储的数据。


它能工作,但并不完全,第一个和最后一个单元格无论如何都会消失。 - QuimeraKoke
comentarios 数组是否在 viewWillAppear 之外被修改了?你能否更新你的问题并添加新的 tableView:cellForRowAtIndexPath 代码,这样我们就可以尝试弄清楚为什么你会有一个没有任何内容的单元格。您可能需要设置断点并检查 Comentario 对象以查看在配置单元格时字段是否具有(nil)值。 - user4151918
是的,你说得对,在滚动之前,Comentario变成了nil,就好像在滚动后我无法保存它。 - QuimeraKoke
如果您想让我们帮助解答为什么会发生这种情况,您需要发布更多的代码,以便我们可以尝试找到问题所在。您能在GitHub上分享您的项目吗? - user4151918

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接