gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:57 24 4
gpt4 key购买 nike

当我在 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问题是当我向下滚动时,单元格的信息变为零,但 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];

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

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

copy is required when the object is mutable. Use this if you need the value of the object as it is at this moment, and you don't want that value to reflect any changes made by other owners of the object. You will need to release the object when you are finished with it because you are retaining the copy.

Weak

weak is similar to strong except that it won't increase the reference count by 1. It does not become an owner of that object but just holds a reference to it. If the object's reference count drops to 0, even though you may still be pointing to it here, it will be deallocated from memory.

This is a good website to learn about strong and weak for iOS 5. http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1

除了上面的问题,你对SimpleTableCell的约束也不对:
enter image description here

您应该转到 Main.storyboard 并检查它。(在 Interface Builder 中选择 Compact Width 和 Compact Height Size Class)

关于ios - 在我的 UITableView 中向下滚动时单元格消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30553648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com