在我的UITableView中向下滚动时,单元格消失

时间:2015-05-31 03:51:12

标签: ios objective-c uitableview

当我在我的表格中向下滚动时,一些单元格内容会消失(标签和图像视图)。 我的代码:

-(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

编辑Nª3 问题是当我向下滚动时,单元格的信息变为零,但是comentarios Array有信息。

编辑Nª4 这是项目https://github.com/QuimeraKoke/BANG-

2 个答案:

答案 0 :(得分:2)

我还有其他一些可以改善您的代码的建议。

您必须使用viewDidAppearviewWillAppear方法拨打超级电话:

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

而不是使用:

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

使用:

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

最后,您应该检查您的dataSource:

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

您的问题已得到解决。

  

复制

     当对象可变时,

需要复制。如果需要,请使用此选项   此时对象的值,你不希望这样   值以反映对象的其他所有者所做的任何更改。您   完成后将需要释放对象,因为   你保留副本。

     

<强>弱

     

弱类似于强类,但它不会增加参考   数量为1.它不会成为该对象的所有者,而只是成立   对它的引用。如果对象的引用计数降为0,则为偶数   虽然你可能仍然在这里指出它,它将被解除分配   来自记忆。

     

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

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

您应该转到Main.storyboard并进行检查。(在界面生成器中选择紧凑宽度和紧凑高度尺寸类别)

答案 1 :(得分: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];

是否有理由使用UIViewController(使用您添加的tableView),而不是简单地使用UITableViewController?

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

制作the changes that Banning suggests后,你可能没问题。滚动后,我看不出为什么单元格会为空白的任何其他原因。

如果仍然发生,您应该发布Comentario课程,以便我们查看该代码的问题是否会影响存储的数据。