我有自定义单元格的tableview,当我想添加一个时,我有一个问题,当我有时重新加载tableview时,最后一个单元格不能完全显示。 但是当我重新启动应用程序时,所有tableviewcell都会正确显示。这个问题最常出现在iPhone 4S上,而在iPhone 6中则更少见。 你知道我想做什么吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row <= [[[DocumentManager sharedManager] listOfFolder] count]) {
FolderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"folderTableViewCell" forIndexPath:indexPath];
if (indexPath.row == [[[DocumentManager sharedManager] listOfFolder] count])
{
cell.labelNameFolder.text = [NSLocalizedString(@"mobile", @"") uppercaseString];
// cell.labelCount.text = [NSString stringWithFormat:@"%d", (int)[thumbsArr count]];
cell.tfNameFolder.text = @"";
cell.tfNameFolder.enabled = false;
cell.labelNameFolder.textColor = kBlackColor;
cell.imagePix.image = nil;
if ([thumbsArr count] > 0)
{
cell.imagePix.image = [thumbsArr objectAtIndex:0];
}
else
{
cell.imagePix.image =[UIImage imageNamed:@"logoPlaceholder"];
}
cell.tag = 0;
}
else
{
NSString *folderName = [[[DocumentManager sharedManager] listOfFolder] objectAtIndex:indexPath.row ];
NSArray *arrayItems = [[DocumentManager sharedManager] listOfItemsForFolder:folderName];
cell.labelNameFolder.text = [folderName uppercaseString];
cell.labelNameFolder.textColor = [UIColor clearColor];
cell.tfNameFolder.text = [folderName uppercaseString];
if ([folderName localizedCompare:NSLocalizedString(@"Non classés", @"")] == NSOrderedSame)
{
cell.tfNameFolder.enabled = false;
}
else
{
cell.tfNameFolder.enabled = tableView.editing;
}
cell.imagePix.image = nil;
cell.tfNameFolder.delegate = self;
cell.tfNameFolder.tag = indexPath.row ;
cell.tag = indexPath.row ;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSInteger index = indexPath.row ;
UIImage *image = [[[DocumentManager sharedManager] listOfItemsForFolder:folderName] count] > 0 ?
[[DocumentManager sharedManager] firstImageOfFolder:folderName]
: [UIImage imageNamed:@"logoPlaceholder"];
CGSize imageSize = image.size;
CGSize viewSize = cell.imagePix.bounds.size; // size in which you want to draw
float hfactor = imageSize.width / viewSize.width;
float vfactor = imageSize.height / viewSize.height;
float factor = fmax(hfactor, vfactor);
// Divide the size by the greater of the vertical or horizontal shrinkage factor
float newWidth = imageSize.width / factor;
float newHeight = imageSize.height / factor;
CGRect newRect = CGRectMake(0,0, newWidth, newHeight);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0f);
//draw
[image drawInRect:newRect];
//capture resultant image
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
if (cell.tag == index)
{
cell.imagePix.image = image;
}
});
});
}
return cell;
}
else {
EndTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"endTableViewCell" forIndexPath:indexPath];
int value = kFolderNumber - (int)[[[DocumentManager sharedManager] listOfFolder] count];
if (value == 0) {
if (WidthDevice < 350)
cell.labelTitre.text = NSLocalizedString(@"messageTooEndFolderShort", nil);
else
cell.labelTitre.text = NSLocalizedString(@"messageTooEndFolder", nil);
}
else if (value == 1)
cell.labelTitre.text = [NSString stringWithFormat:NSLocalizedString(@"create %d folder", nil), value];
else
cell.labelTitre.text = [NSString stringWithFormat:NSLocalizedString(@"create %d folders", nil), value];
[cell.buttonAchatInApp addTarget:self action:@selector(buttonAchatInApp:) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ([[[DocumentManager sharedManager] listOfFolder] count] + 2);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120;
}
我通过这种方法获得了新细胞
[[DocumentManager sharedManager] createFolder:self.textField.text];
- (void)createFolder:(NSString *)folderName
{
folderName = [folderName stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingFormat:@"/Test/%@/%@", [PFUser currentUser].username, folderName]; NSError *error = nil;
}
答案 0 :(得分:0)
尝试在向[[[DocumentManager sharedManager] listOfFolder] count]
添加新对象之前和之后记录listOfFolder
。当您在tableView上调用reloadData
时,它可能不会更新。