UITableView没有向下滚动

时间:2014-07-19 13:50:26

标签: ios objective-c uitableview

我创建了一个ios应用程序,其中我有一个动态单元格生成表。当我尝试向下滚动到桌子。它返回到表的顶部。我的代码如下:

-(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;
}

这里是cellForRowAtIndexPath:

-(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;
}

提前致谢。

1 个答案:

答案 0 :(得分:2)

您不应该在索引路径的行中将单元格的高度出列。此外,self.customCell意味着您只使用一个单元格。这是一种非常奇怪和不寻常的模式。

此外,每个回收单元的所有sqlite调用都是非常低效的。相反,将所需的数据提取到数组中并使用它。

另外,请避免在单元格方法中分配格式化程序等。相反,请使用static变量。