滚动向下时,有3个部分的tableview崩溃了

时间:2014-07-30 09:29:14

标签: ios nsmutablearray tableview sections

我正在创建一个包含3个部分的tableview,每个部分都是从Multidimentional NSMutableArray填充的。这些值被添加到可变数组中,但每次我尝试将文本设置为每个部分中的标签时,应用程序都会崩溃。这是代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"wsolna hon");
static NSString *cellIdentifier = @"HomeTableViewCell";
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

// Configure the cell...
int row= [indexPath row];


                if (indexPath.section == 0){
                     homeObject = [homeArray[0] objectAtIndex:indexPath.row];

                        NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[homeObject.News dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
                    cell.NewsDateLabel.text= homeObject.News_Date;
                    cell.NewsLabel.attributedText= attrStr;
                    NSLog(@"news: %@", homeObject.News);

                }


                if (indexPath.section == 1){

                    homeObject = [homeArray[1] objectAtIndex:indexPath.row];

                   NSLog(@"value of indexpath for library: %d",row);
                   cell.NewsLabel.text= homeObject.Library_text;
                   cell.TestimonialNameLabel.text= @"";
                NSLog(@"Library: %@", homeObject.Library_text);
            }

                if (indexPath.section == 2){
                    homeObject = [homeArray[2] objectAtIndex:indexPath.row];

                     NSLog(@"news: %@",homeObject.Testimonial_Description);
                     cell.NewsLabel.text= homeObject.Library_text;
                     cell.TestimonialNameLabel.text = homeObject.Testimonial_Name;
                    NSLog(@"Testimonial: %@", homeObject.Testimonial_Description);

            }



  return cell;
 }

//这里是填充NSMutable数组的地方       - (void)getDataFromDb      {

NSString * paths=[self getWritableDBPath];

const char *dbpath =  [paths UTF8String];
sqlite3_stmt    *statement;
static sqlite3 *database = nil;

if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{

    NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM homedata", nil];

    const char *query_stmt = [querySQL UTF8String];

    //  NSLog(@"Databasae opened = %@", userN);

    if (sqlite3_prepare_v2(database,
                           query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {

        while(sqlite3_step(statement) == SQLITE_ROW)
        {


            NSString *modID= [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
            NSString *nDate = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
            NSString *nText = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
            NSString *dText = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
            NSString *tText = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
            NSString *tName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];

            if ([modID isEqualToString:@"77"]){
              //  NSLog(@"News found: %@", nText);
                [homeArray[0] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
            } else if ([modID isEqualToString:@"81"]){
                [homeArray[1] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
              //   NSLog(@"Library found: %@", dText);
            } else if ([modID isEqualToString:@"78"]){
                [homeArray[2] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
              //   NSLog(@"News found: %@", tText);
            }
        }
        sqlite3_finalize(statement);
    }
    //  NSLog( @"Save Error: %s", sqlite3_errmsg(database) );

    sqlite3_close(database);
}
 }

在ViewDidLoad中我有:

homeArray = [[NSMutableArray alloc] initWithObjects:[[NSMutableArray alloc] init], [[NSMutableArray alloc] init], [[NSMutableArray alloc] init], nil];

还有:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
    NSLog(@"woslit 3al 1");
    NSString *querySQL = [NSString stringWithFormat: @"SELECT count(*) FROM homedata where module_ID= 77"];
    return [self countcells:querySQL];
}
if(section == 1)
{
      NSLog(@"woslit 3al 2");
    NSString *querySQL = [NSString stringWithFormat: @"SELECT count(*) FROM homedata where module_ID= 78"];
    return  [self countcells:querySQL];
}

if (section == 2)
{
     NSLog(@"woslit 3al 3");
  return[self countcells:@"SELECT count(*) FROM homedata where module_ID= 81"];
}
 else return 0;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

//TEMP SECTION STRING HOLDER:
NSString *sectionHeader = nil;


//SET TITLE FOR EACH SECTION:
if(section == 0) {
    sectionHeader = @"News";
}

if(section == 1) {
    sectionHeader = @"TYM Library";
}

if(section == 2) {
    sectionHeader = @"Testimonial";
}

我可以在使用NSLog时获取值。但是当我向下滑动它崩溃了。 有人可以检查问题可能在哪里吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

您的numberOfRowsInSection数据源委托方法应返回homeArray子数组中的元素数,而不是数据库中的元素数。这要快得多,并避免数据库和内存版本的数据不同步的问题:

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return [homeArray[section] count];
}