[英]How to manage UITableView if number of sections are not confirmed
我正在使用UITableView,其中根据数据动态创建了多个部分。 从1到12。节名称是月份的名称。 如何检查wrt到各节的名称。 我的代码是
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [sectionArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionArray objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = 0;
for(int counter=0; counter<[sectionArray count]; counter++)
{
NSString *month = [sectionArray objectAtIndex:counter];
if([month isEqualToString:@"October"])
{
count = [octoberArray count];
}
else if([month isEqualToString:@"November"])
{
count = [novemberArray count];
}
else if([month isEqualToString:@"December"])
{
count = [decemberArray count];
}
else if([month isEqualToString:@"January"])
{
count = [januaryArray count];
}
else if([month isEqualToString:@"February"])
{
count = [febuaryArray count];
}
else if([month isEqualToString:@"March"])
{
count = [marchArray count];
}
else if([month isEqualToString:@"April"])
{
count = [aprilArray count];
}
else if([month isEqualToString:@"May"])
{
count = [mayArray count];
}
else if([month isEqualToString:@"June"])
{
count = [juneArray count];
}
else if([month isEqualToString:@"July"])
{
count = [julyArray count];
}
else if([month isEqualToString:@"August"])
{
count = [augustArray count];
}
else
{
count = [septemberArray count];
}
}
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HistoryCell *cell = (HistoryCell *)[tableView dequeueReusableCellWithIdentifier:@"historyCell"];
if(cell == nil)
{
cell = [[[NSBundle mainBundle]loadNibNamed:@"HistoryCell" owner:self options:nil] objectAtIndex:0];
}
for(int counter=0; counter<[sectionArray count]; counter++)
{
NSString *month = [sectionArray objectAtIndex:counter];
if([month isEqualToString:@"October"])
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[octoberGlassesArray objectAtIndex:indexPath.row]];
cell.unitLabel.text = @"Unit";
cell.dateLabel.text = [octoberArray objectAtIndex:indexPath.row];
}
else if([month isEqualToString:@"November"])
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[novemberGlassesArray objectAtIndex:indexPath.row]];
cell.dateLabel.text = [novemberArray objectAtIndex:indexPath.row];
}
else if([month isEqualToString:@"December"])
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[decemberGlassesArray objectAtIndex:indexPath.row]];
cell.dateLabel.text = [decemberArray objectAtIndex:indexPath.row];
}
else if([month isEqualToString:@"January"])
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[januaryGlassesArray objectAtIndex:indexPath.row]];
cell.dateLabel.text = [januaryArray objectAtIndex:indexPath.row];
}
else if([month isEqualToString:@"February"])
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[febuaryGlassesArray objectAtIndex:indexPath.row]];
cell.dateLabel.text = [febuaryArray objectAtIndex:indexPath.row];
}
else if([month isEqualToString:@"March"])
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[marchGlassesArray objectAtIndex:indexPath.row]];
cell.dateLabel.text = [marchArray objectAtIndex:indexPath.row];
}
else if([month isEqualToString:@"April"])
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[aprilGlassesArray objectAtIndex:indexPath.row]];
cell.dateLabel.text = [aprilArray objectAtIndex:indexPath.row];
}
else if([month isEqualToString:@"May"])
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[mayGlassesArray objectAtIndex:indexPath.row]];
cell.dateLabel.text = [mayArray objectAtIndex:indexPath.row];
}
else if([month isEqualToString:@"June"])
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[juneGlassesArray objectAtIndex:indexPath.row]];
cell.dateLabel.text = [juneArray objectAtIndex:indexPath.row];
}
else if([month isEqualToString:@"July"])
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[julyGlassesArray objectAtIndex:indexPath.row]];
cell.dateLabel.text = [julyArray objectAtIndex:indexPath.row];
}
else if([month isEqualToString:@"August"])
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[augustGlassesArray objectAtIndex:indexPath.row]];
cell.dateLabel.text = [augustArray objectAtIndex:indexPath.row];
}
else
{
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[septemberGlassesArray objectAtIndex:indexPath.row]];
cell.dateLabel.text = [septemberArray objectAtIndex:indexPath.row];
}
}
return cell;
}
应用程序崩溃。 如何解决此问题?
我确定您不太清楚UITableViewDataSource
是如何工作的。 但是,如果我正确理解您的想法,则下面的代码应该可以解决问题:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [sectionArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *yourMonthArray = //Here get an array for adequate month
return [yourMonthArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *arr = @[@"January",@"February",...,@"December"];
// asserts are optional. Just for sure you initialized your data well.
NSAssert(arr.count == 12, @"The number of months is not valid");
NSAssert(arr.count == [sectionArray count], @"Number of months is different than number of sections");
return arr[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HistoryCell *cell = (HistoryCell *)[tableView dequeueReusableCellWithIdentifier:@"historyCell"];
if(cell == nil)
{
// Please consider using UINib instread.
cell = [[[NSBundle mainBundle]loadNibNamed:@"HistoryCell" owner:self options:nil] objectAtIndex:0];
}
NSString *month = [sectionArray objectAtIndex:indexpath.section];
cell.glassesLabel.text = [NSString stringWithFormat:@"%@ Glasses",[octoberGlassesArray objectAtIndex:indexPath.section]];
cell.unitLabel.text = @"Unit";
NSArray *yourMonthArray = //Here get an array for adequate month
cell.dateLabel.text = yourMonthArray[indexPath.row];
return cell;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.