iOS의 UITableView에 JSON 데이터를 표시하는 애플리케이션을 만들고 싶습니다. 내 웹 서비스에는 3 ~ 4 페이지가 포함되어 있습니다. 테이블 뷰가 다음 페이지 데이터를 스크롤 할 때 스크롤을 원합니다. 그럼 내가 코드를 그더 많은 JSON 데이터를 iOS의 페이지 매김과 함께로드하십시오.
- (void)viewDidLoad
{
pagenum=1;
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.truemanindiamagazine.com/webservice/news.php?page=%d",pagenum]];
[self.newsTable setShowsHorizontalScrollIndicator:NO];
[self.newsTable setShowsVerticalScrollIndicator:NO];
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
data = [NSData dataWithContentsOfURL: url];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.dataArray=[_json objectForKey:@"data"];
if (self.dataArray.count > 0)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.newsTable reloadData];
});
}
NSLog(@"images,%@",self.dataArray);
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(TableCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *[email protected]"Cell";
TableCell *cell=[tableView dequeueReusableCellWithIdentifier:Cellidentifier];
if (cell ==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
{
NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.section];
NSString *img2=[dict valueForKey:@"post_image"];
[cell.newsImage sd_setImageWithURL:[NSURL URLWithString:img2] placeholderImage:[UIImage imageNamed:@"Hisoka.jpg"]];
NSString *title=[dict valueForKey:@"post_title"];
cell.headLabel.text=title;
NSString *content=[dict valueForKey:@"post_content"];
cell.descripLabel.text=content;
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *date=[dict valueForKey:@"post_date"];
NSDate * dateNotFormatted = [dateFormatter dateFromString:date];
[dateFormatter setDateFormat:@"d-MMM-YYYY"];
NSString * dateFormatted = [dateFormatter stringFromDate:dateNotFormatted];
cell.dateLabel.text=dateFormatted;
}
return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
pagenum=pagenum+1;
[self getData];
}
-(void)getData {
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.truemanindiamagazine.com/webservice/news.php?page=%d",pagenum]];
dispatch_async(kBgQueue, ^{
data = [NSData dataWithContentsOfURL:url];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary=[self.dataArray objectAtIndex:indexPath.section];
NSString *url=[dictionary valueForKey:@"link"];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
다음 문제 내가 스크롤 할 때 테이블은 다음 페이지의 데이터 만 첫 페이지 데이터가 내가 너무 솔루션을 나에게 I을주세요 테이블보기에 모든 페이지 데이터를 유지하려면 제거를 보여줍니다 볼 수 있다는 것입니다 이 질문은 과거에 물었다는 것을 알고 있지만 나를 위해 일하지 못했습니다. 그래서 제게 해결책을주십시오.
thnx 늦게 답변을 드려 죄송합니다. – Ramesh
하지만 문제는 그 때 나는이 줄에서 오류가 점점 더 많이 발생한다는 것입니다. [self.json objectForKey : @ "data"]]; 및 오류 : "배열 인수가 NSArray 아닙니다." – Ramesh
np, 환영합니다 :). 귀하의 josn 객체가 데이터가있을 때마다 키 "데이터"에 대한 배열을 반환합니까? 아니면 그렇지 않으면 null, 그렇지 않으면 배열과 다른 구조를 반환합니까? –