首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >区段数未确认时如何管理UITableView

区段数未确认时如何管理UITableView
EN

Stack Overflow用户
提问于 2014-10-03 18:50:39
回答 1查看 156关注 0票数 0

我使用的是UITableView,它根据数据动态创建多个部分。从1到12。分区名称是月份的名称。如何将w.r.t检查为节名称。我的代码是

代码语言:javascript
复制
- (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;
}

应用程序崩溃。如何解决此问题?

EN

回答 1

Stack Overflow用户

发布于 2014-10-03 18:59:30

我相信您并不清楚UITableViewDataSource是如何工作的。但是如果我正确理解了你的想法,下面的代码应该可以解决这个问题:

代码语言:javascript
复制
- (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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26177623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档