簡體   English   中英

在UITableView中向下滾動時,單元格消失

[英]Cells disappear when scroll down in my UITableView

當我在tableView中向下滾動時,單元格的某些內容消失了(標簽和imageViews)。 我的代碼:

-(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問題是當我向下滾動時,單元格的信息變為nil,但comentarios Array有此信息。

編輯Nª4,這是項目https://github.com/QuimeraKoke/BANG-

我還有其他一些建議可以改善您的代碼。

您必須在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];

最后,您應該檢查您的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的約束也不正確:
在此處輸入圖片說明

您應該轉到Main.storyboard並進行檢查。(在Interface Builder中選擇Compact Width和Compact Height Size類)

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,然后將其IBOutlet連接到情節提要單元格的標簽,圖像和按鈕。 這將消除必須使用標簽的麻煩。

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?

UITableViewController知道如何調整其插圖以解決頂部和底部的條(您可以將其automaticallyAdjustsScrollViewInsets設置為YES )。

進行Banning建議的更改后,您可能會好的。 我看不到任何其他原因,導致滾動后單元格為空白。

如果仍然發生這種情況,則應該發布Comentario類,這樣我們就可以查看該代碼是否有問題正在影響存儲的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM