ios - 使用部分类别填充表格 View
<p><p>我想在 uitableview 中填充数据。我有不同类别的不同部分,每个类别包含不同数量的行。 </p>
<pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return allCategory.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *heading = ;
return heading;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
int count = 0;
NSString *heading = ;
for (Product * catin productArry) {
if () {
count ++;
}
}
return count;
}
</code></pre>
<p>这是我根据类别返回多行的代码。
例如,我有一个类别名称“Engine”,它返回一个行数和一个类别名称“Blocks”,它返回 2 个行数。现在我想针对“cellForRowAtIndexPath”方法中的每个类别填充数据。如何针对每个类别填充正确的数据?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong>选项 1:</strong> </p>
<pre><code>- (Product *)dataForRowAtIndexPath:(NSIndexPath *)indexPath {
int count = 0;
NSString *heading = ;
for (Product * productin self.productArry) {
if () {
if (count == indexPath.row) {
return product;
}
count++;
}
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Product *product = ;
return nil;
}
</code></pre>
<p><strong>选项 2:</strong>
或者您可能想要制作一个过滤数组</p>
<p>.h</p>
<pre><code>@property (nonatomic, strong) NSArray *allCategory;
@property (nonatomic, strong) NSArray *productArry;
@property (nonatomic, strong) NSArray *products;
</code></pre>
<p>.m</p>
<pre><code>- (NSArray *)products {
if (!_products) {
NSMutableArray *arrayCategoryProductArray = [init];
for(int i =0 ;i< self.allCategory.count;i++){
NSString *strCat = ;
NSArray *filteredarray = ];
;
}
_products = arrayCategoryProductArray;
}
return _products;
}
</code></pre>
<p>输入</p>
<pre><code><__NSArrayI 0x7866c8b0>(
<Product: 0x78668460; category = A; name = A1>,
<Product: 0x78663280; category = B; name = B1>,
<Product: 0x7866c750; category = A; name = A2>,
<Product: 0x7864c950; category = B; name = B2>,
<Product: 0x786610a0; category = B; name = B3>
)
</code></pre>
<p>输出</p>
<pre><code><__NSArrayM 0x798449b0>(
<__NSArrayI 0x79839050>(
<Product: 0x78668460; category = A; name = A1>,
<Product: 0x7866c750; category = A; name = A2>
)
,
<__NSArrayI 0x798c7310>(
<Product: 0x78663280; category = B; name = B1>,
<Product: 0x7864c950; category = B; name = B2>,
<Product: 0x786610a0; category = B; name = B3>
)
)
</code></pre>
<p>所以</p>
<pre><code>- (Product *)dataForRowAtIndexPath:(NSIndexPath *)indexPath {
Product *product = [ objectAtIndex:indexPath.row];
return product;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Product *product = ;
return nil;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 使用部分类别填充表格 View ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36329645/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36329645/
</a>
</p>
页:
[1]