在UITableView Xcode iOS中滚动时单元格文本重叠
每次我开始在在UITableView Xcode iOS中滚动时单元格文本重叠,ios,objective-c,uitableview,Ios,Objective C,Uitableview,每次我开始在tableView中滚动时,字幕标签文本在每一行中都会相互重叠。在过去的4个小时里,我尝试了在互联网上搜索我点击并尝试过的每一个问题 这是我的ViewController.m: @interface ANViewController() { NSMutableArray *TitleLabel; NSMutableArray *SubtitleLabel; NSMutableArray *AvatarImages; } @end @implement
tableView
中滚动时,字幕标签文本在每一行中都会相互重叠。在过去的4个小时里,我尝试了在互联网上搜索我点击并尝试过的每一个问题
这是我的ViewController.m
:
@interface ANViewController()
{
NSMutableArray *TitleLabel;
NSMutableArray *SubtitleLabel;
NSMutableArray *AvatarImages;
}
@end
@implementation ANViewController
@synthesize thetableview;
- (void)viewDidLoad
{
[super viewDidLoad];
self.thetableview.delegate = self;
self.thetableview.dataSource = self;
TitleLabel = [NSMutableArray array];
SubtitleLabel = [NSMutableArray array];
AvatarImages = [NSMutableArray array];
__block NSArray *posts;
__block NSData *allNewsData = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0ul);
dispatch_async(queue, ^{
allNewsData = [NSData dataWithContentsOfURL:[NSURL URLWithString: API_URL]];
NSError *error;
NSMutableDictionary *allNews = [NSJSONSerialization JSONObjectWithData:allNewsData options:NSJSONReadingMutableContainers error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
} else {
posts = allNews[@"data"];
for (NSDictionary *newsPost in posts) {
[TitleLabel addObject:newsPost[@"title"]];
[SubtitleLabel addObject:newsPost[@"post_message"]];
NSString *avatarUrl = AVATAR_URL;
NSString *avatarExt = @".jpg";
[AvatarImages addObject:[NSString stringWithFormat:@"%@%@%@", avatarUrl, newsPost[@"user_id"], avatarExt]];
}
}
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"THIS IS THE MAIN THREAD...");
[self.thetableview reloadData];
});
});
NSURL *url = [NSURL URLWithString: WEBSITE_URL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
long count = TitleLabel.count;
if(count == 0){
count = 1;
}
return count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier;
ANTableViewCell *Cell;
if(TitleLabel.count == 0){
NSLog(@"Did with no news");
CellIdentifier = @"Cell_NoNews";
Cell = [thetableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil) {
Cell = [[ANTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Cell.noNewsLabel.text = @"No news to display!";
}else{
NSLog(@"Did with news");
CellIdentifier = @"Cell";
Cell = [thetableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil) {
Cell = [[ANTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Cell.TitleLabel.text = [TitleLabel objectAtIndex:indexPath.row];
Cell.SubtitleLabel.text = [SubtitleLabel objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:[AvatarImages objectAtIndex:indexPath.row]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
Cell.AvatarImage.image = img;
}
return Cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
只需将
UITableViewCellStyleSubtitle
更改为UITableViewCellStyleDefault
,您就需要一个异步连接,而不是像现在这样的同步连接:NSData*allNewsData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:API_URL]]代码>在阻止主线程的viewDidLoad
方法中
您可以根据需要修改此代码,以便在后台下载:
__block NSData *allNewsData = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
allNewsData = [NSData dataWithContentsOfURL:[NSURL URLWithString: API_URL]];
NSURL *url = [NSURL URLWithString: WEBSITE_URL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"THIS IS THE MAIN THREAD...");
[self.thetableview reloadData];
});
});
试着增加行高看看会发生什么。那是哪里?我没有看到它。在你的故事板或xib文件上。仍然是相同的问题。为什么你[Cell removeFromSuperview]?尝试删除此行。