我有一个UITableView和sqlite数据库。 textLabel和detailTextLabel以及图像来自db。 当我删除一行然后添加一行(添加到'array'然后'reloadData')时,'textLabel'获取旧数据。 我在日志中看到的是正确的数据应该在标签中,但旧数据在标签中。 我检查了数据来自db,一切都是正确的。
我的代码:
ADD GROUP:
NSString *userID = [NSString stringWithFormat:@"%i",[[NSUserDefaults standardUserDefaults] integerForKey:@"userID"]];
NSString *groupName = [alertView textFieldAtIndex:0].text;
NSString *mobile = [[NSUserDefaults standardUserDefaults] objectForKey:@"userPhoneNumber"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:userID forKey:@"userID"];
[parameters setObject:groupName forKey:@"groupName"];
[parameters setObject:mobile forKey:@"mobile"];
@try {
NSString *responseXML = HTTP_POST(WEB_SERVICES_URL, @"addGroup", parameters);
NSLog(@"\nResponse XML (addGroup):\n%@\n", responseXML);
Group *aGroup = [[Group alloc] init];
aGroup.groupID = [[IDAN_XML_Parser sharedInstance] getResponseFor:@"answer" inXML:responseXML].integerValue;
aGroup.visible = kTrue;
aGroup.manager = [[NSUserDefaults standardUserDefaults] integerForKey:@"userID"];
aGroup.name = groupName;
aGroup.time = [[MobileControlHandler sharedInstance] getFormattedDate];
[[SQLiteHandler sharedDatabase] addGroup:aGroup];
[CoreDataHandler getGroups];
[SVProgressHUD dismiss];
listOfGroups = [[NSMutableArray alloc] init];
listOfGroups = [[SQLiteHandler sharedDatabase] getAllGroups];
[tableGroup reloadData];
}
@catch (NSException *exception) {
NSLog(@"addGroup Exception: %@", exception.reason);
}
@finally {}
删除行:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = self.view.center;
[indicator startAnimating];
[tableView beginUpdates];
@try {
Group *aGroup = listOfGroups[indexPath.row];
NSMutableArray *allMembersInGroup = [[SQLiteHandler sharedDatabase] getAllMembersInGroupID:aGroup.groupID];
NSString *membersIDs = @"";
printf("allMembersInGroup.count: %i\n", allMembersInGroup.count);
if (allMembersInGroup.count > 0) {
for (int i = 0; i < allMembersInGroup.count; i++) {
GroupMember *aGroupMember = allMembersInGroup[i];
membersIDs = [membersIDs stringByAppendingString:[NSString stringWithFormat:@"%i,", aGroupMember.memberID]];
}
membersIDs = [membersIDs substringToIndex:membersIDs.length - 1];
[CoreDataHandler deleteGroupMemberID:membersIDs inGroupID:[NSString stringWithFormat:@"%i", aGroup.groupID]];
}
[CoreDataHandler getGroups];
[tableView endUpdates];
[tableView setEditing:NO animated:YES];
listOfGroups = [[SQLiteHandler sharedDatabase] getAllGroups];
[tableView reloadData];
[tableView setNeedsLayout];
[SVProgressHUD dismiss];
[indicator stopAnimating];
indicator.hidden = YES;
}
@catch (NSException *exception) {
NSLog(@"commitEditingStyle:forRowAtIndexPath:%i, Exception: %@", indexPath.row, exception.reason);
[SVProgressHUD dismiss];
[indicator stopAnimating];
indicator.hidden = YES;
}
@finally {}
}
}
这就是我配置单元格的方式:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
UILabel *textLabel = (UILabel*)[cell.contentView viewWithTag:31];
UILabel *detailTextLabel = (UILabel*)[cell viewWithTag:32];
Group *newGroup = listOfGroups[indexPath.row];
cell.backgroundColor = darkGrayColor();
textLabel.text = newGroup.name;
NSLog(@"newGroup.name: %@, textLabel.text: %@", newGroup.name, textLabel.text);
textLabel.font = [UIFont fontWithName:kFontNameBold size:kFontSizeBig];
textLabel.tag = kTagLabelGroupName;
textLabel.backgroundColor = [UIColor clearColor];
textLabel.textAlignment = NSTextAlignmentRight;
textLabel.textColor = [UIColor whiteColor];
detailTextLabel.font = [UIFont fontWithName:kFontName size:kFontSize];
detailTextLabel.text = newGroup.time;
detailTextLabel.textColor = [UIColor whiteColor];
detailTextLabel.tag = kTagLabelGroupDate;
detailTextLabel.backgroundColor = [UIColor clearColor];
detailTextLabel.textAlignment = NSTextAlignmentRight;
// cell.imageView.image = self.imageGroup.image;
cell.imageView.layer.cornerRadius = 25;
cell.imageView.layer.borderWidth = 0;
cell.imageView.layer.borderColor = (__bridge CGColorRef)([UIColor clearColor]);
cell.imageView.clipsToBounds = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *image = [CoreDataHandler decodeImageFromBase64:newGroup.picture];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = [CoreDataHandler imageWithImage:image scaledToSize:CGSizeMake(50, 50)];
[cell setNeedsLayout];
});
});
/* UIImageView *imageGroupManager = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 25, 25)];
imageGroupManager.image = [UIImage imageNamed:@"star.png"];
imageGroupManager.tag = kTagPictureStar;
if (newGroup.manager == [[NSUserDefaults standardUserDefaults] integerForKey:@"userID"]) {
[cell insertSubview:imageGroupManager aboveSubview:cell.imageView];
} */
return cell;
}
修改
我将自制的textLabel更改为cell.textLabel并且一切顺利,但问题是我无法更改文本方向,可能将其更改为自定义单元格应该没问题。
答案 0 :(得分:1)
尝试改变:
原始代码:
[CoreDataHandler getGroups];
[tableView endUpdates];
[tableView setEditing:NO animated:YES];
listOfGroups = [[SQLiteHandler sharedDatabase] getAllGroups];
[tableView reloadData];
[tableView setNeedsLayout];
[SVProgressHUD dismiss];
[indicator stopAnimating];
indicator.hidden = YES;
更改了代码:
[CoreDataHandler getGroups];
listOfGroups = [[SQLiteHandler sharedDatabase] getAllGroups];
[tableView setEditing:NO animated:YES];
[tableView endUpdates];
[SVProgressHUD dismiss];
[indicator stopAnimating];
indicator.hidden = YES;
答案 1 :(得分:0)
UITableViewCell的子类化解决了我的问题。