ios - 无法从 json 响应中设置部分中的 UICollectionView 项目数
<p><p>我对 iOS 编程非常陌生,正在尝试从 json 响应中设置 UICollectionView 项目。 </p>
<p>这是我的代码:</p>
<pre><code>- (void)viewDidLoad
{
;
;
/* uncomment this block to use subclassed cells */
forCellWithReuseIdentifier:@"cvCell"];
/* end of subclass-based cells block */
// Configure layout
UICollectionViewFlowLayout *flowLayout = [ init];
;
;
;
;
}
-(void) downloadJSONData {
NSURL *serviceUrl = [initWithString:@"http://myurlhere.com/"];
NSURLRequest *request = ;
NSURLConnection *conn = [ initWithRequest:request delegate:self];
;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.data = [init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)responsedata {
;
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error : %@", error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Succeeded! Received %d bytes of data",);
NSString *txt = [ initWithData:self.data encoding: NSASCIIStringEncoding];
NSLog(@"response data %@", txt);
self.dictionaryData = ;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//NSMutableArray *sectionArray = ;
self.jsonArray = ;
return ;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// Setup cell identifier
static NSString *cellIdentifier = @"cvCell";
/*Uncomment this block to use nib-based cells */
// UICollectionViewCell *cell = ;
// UILabel *titleLabel = (UILabel *);
// ;
/* end of nib-based cell block */
/* Uncomment this block to use subclass-based cells */
CVCell *cell = (CVCell *);
NSMutableArray *data = ;
NSString *cellData = ;
;
/* end of subclass-based cells block */
// Return the cell
return cell;
}
</code></pre>
<p>当我运行代码时,它什么也不显示。我想要的是它显示项目并且数量等于数组的计数。还有一件事,关于如何将图像异步加载到 Collection View 中的任何建议? </p>
<p>对不起我的英语。如果有人可以帮助我,我会很高兴。 </p>
<p>谢谢。干杯!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在<code>connectionDidFinishLoading</code>方法中添加:</p>
<pre><code>;
</code></pre>
<p>对于 Asyn 图像加载,您可以检查以下任何一项,</p>
<ol>
<li> <a href="https://github.com/rs/SDWebImage" rel="noreferrer noopener nofollow">SDWebImage</a> </li>
<li> <a href="https://github.com/nicklockwood/AsyncImageView" rel="noreferrer noopener nofollow">AsyncImageView</a> </li>
<li> <a href="http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html" rel="noreferrer noopener nofollow">LazyTableImages</a> - 它适用于 UITableView,但您可以对 UICollectionView 使用相同的概念</li>
</ol></p>
<p style="font-size: 20px;">关于ios - 无法从 json 响应中设置部分中的 UICollectionView 项目数,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/15126918/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/15126918/
</a>
</p>
页:
[1]