2014-07-19 4 views
-1

동적 셀 생성이있는 테이블이있는 ios 앱을 만들었습니다. 내가 테이블로 내려 가려고 할 때. 테이블의 맨 위로 돌아갑니다. 내 코드는 다음과 같이 다음은 cellForRowAtIndexPathUITableView가 아래로 스크롤되지 않습니다

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(!self.customCell){ 
    self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 
} 

//Cell configuration 
int quoteIndex = indexPath.row % [vendor count]; 
self.customCell.description.text = message[quoteIndex]; 

//Cell Layout 
[self.customCell.description sizeToFit]; 

//Height of cell 
float height = (CGRectGetMaxY(self.customCell.description.frame) +5); 

return height + 1; 
} 

: 사전에

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
CustomTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 
const char *dbpath = [_databasePath UTF8String]; 
sqlite3_stmt *statement; 

if (sqlite3_open(dbpath, &_beaconDB) == SQLITE_OK) 
{ 
    NSString *querySQL = [NSString stringWithFormat: 
          @"SELECT vendor_name, enter_message, enter_image, vendor_image, received_date, time_interval FROM beacons WHERE id=%@", [unique objectAtIndex:indexPath.row]]; 
    const char *query_stmt = [querySQL UTF8String]; 
    if (sqlite3_prepare_v2(_beaconDB, 
         query_stmt, -1, &statement, NULL) == SQLITE_OK) 
    { 
     if (sqlite3_step(statement) == SQLITE_ROW) 
     { 
      title = [[NSString alloc] 
           initWithUTF8String: 
           (const char *) sqlite3_column_text(
                   statement, 0)]; 
      description = [[NSString alloc] 
          initWithUTF8String:(const char *) 
          sqlite3_column_text(statement, 1)]; 

      NSString *fullImage = [[NSString alloc] 
          initWithUTF8String:(const char *) 
          sqlite3_column_text(statement, 2)]; 

      [fullImg insertObject:fullImage atIndex:indexPath.row]; 

      NSString *vendorImage = [[NSString alloc] 
            initWithUTF8String:(const char *) 
            sqlite3_column_text(statement, 3)]; 

      [thumbnailImg insertObject:vendorImage atIndex:indexPath.row]; 

      NSString *receivedDate = [[NSString alloc] 
            initWithUTF8String:(const char *) 
            sqlite3_column_text(statement, 4)]; 

      NSString *timeInterval = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)]; 

      BeaconAdTime *beaconAdTime = [[BeaconAdTime alloc] init]; 
      NSDate *updatedTime = [beaconAdTime updatedDateTime:receivedDate andInterval:[timeInterval intValue]]; 

      int count = 0; 

      do 
      { 
       NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
       [dateFormatter setDateFormat: @"MMM-dd hh:mm a"]; 
       BeaconAdTime *beaconAdTime = [[BeaconAdTime alloc] init]; 
       NSString *presentDateTime = [beaconAdTime presentDateTime]; 
       NSDate *presentDateConvert = [dateFormatter dateFromString:presentDateTime]; 
       count++; 

       if([updatedTime compare:presentDateConvert] == NSOrderedAscending || count == 1) 
       { 
        cell.title.text = title; 
        cell.description.text = description; 
        cell.receivedDate.text = receivedDate; 
        NSString *imgURL = fullImg[indexPath.row]; 
        NSURL *imageURL = [NSURL URLWithString:imgURL]; 
        UIImage *image = nil; 
        image = [UIImage imageWithData:[NSData dataWithContentsOfURL: imageURL]]; 


        cell.vendorImage.image = image; 
        NSUserDefaults *userDefauts = [NSUserDefaults standardUserDefaults]; 
        [userDefauts setObject:fullImg forKey:@"fullImage"]; 
        [userDefauts synchronize]; 
        UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:localNotif]; 
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:[NSString stringWithFormat:@"%d", indexPath.row]]; 
        NSString *uid = [NSString stringWithFormat:@"%d", indexPath.row]; 
        NSDictionary *infodict = [NSDictionary dictionaryWithObject:uid forKey:@"id"]; 
        localNotif.userInfo = infodict; 
        NSDate *fireTime = [[NSDate date] addTimeInterval:0]; 
        localNotif.fireDate = fireTime; 
        localNotif.alertBody = description; 
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
        if(count>1) 
        { 
        break; 
        } 
       } 
      } 
      while (updatedTime!=nil); 
     } 
    sqlite3_finalize(statement); 
    } 
    sqlite3_close(_beaconDB); 
} 
// cell.title.text = vendor[indexPath.row]; 
//  
// cell.vendorImage.image = [UIImage imageNamed:@"gg.jpg"]; 

return cell; 
} 

감사합니다.

답변

2

색인 경로에있는 행의 높이에서 셀의 대기열을 비우지 않아야합니다. 또한 self.customCell은 하나의 셀로 작업하고 있음을 의미합니다. 이것은 매우 이상하고 특이한 패턴입니다.

또한 각 재활용 된 셀에 대한 모든 sqlite 호출은 매우 비효율적입니다. 대신 필요한 데이터를 배열에 가져 와서 작업하십시오.

또한 셀 메서드에 포맷터 등을 할당하지 마십시오. 대신 static 변수를 사용하십시오.

관련 문제