我有两个 TableView ,在第一个 UITableView (联系人 TableView )中加载了所有单元格,当我触摸一个单元格时,它会转到详细联系人,一切都很好。
在第二个 UITableView (详细联系人)中,当我触摸单元格(调用联系人)和第二个 UITableView 方法 didSelectRowAtIndexPath 执行时,它可以工作但会导致所有数据(联系人)在第一个 UITableView 中消失。我该如何解决?
附:
(如果我在第二个 UITableView 中禁用 didSelectRowAtIndexPath ,数据将保留在第一个 TableView 中)
(表格 View 之间没有segue)
第一个UITableView :
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
static NSString *kCellId = @"UIContactCell";
UIContactCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
//cell.imageLabel.alpha=0;
cell.imageView.image=nil;
if (cell == nil) {
cell = [[[UIContactCell alloc] initWithIdentifier:kCellId] autorelease];
cell.imageView.image=nil;
// Background View
// UACellBackgroundView *selectedBackgroundView = [[[UACellBackgroundView alloc] initWithFrame:CGRectZero] autorelease];
// cell.selectedBackgroundView = selectedBackgroundView;
// [selectedBackgroundView setBackgroundColorINPHONE_TABLE_CELL_BACKGROUND_COLOR];
}
OrderedDictionary *subDic = addressBookMap[[addressBookMap keyAtIndex: indexPath.section]];
NSString *key = subDic.allKeys[indexPath.row];
ABRecordRef contact = subDic[key];
CFDataRef imageData = ABPersonCopyImageData(contact);
UIImage *image = nil;
image = [UIImage imageWithDataNSData *)imageData];
// Cached avatar
id data = avatarMap[@(ABRecordGetRecordID(contact))];
if(data == nil) {
//CFRelease(imageData);
//image = [FastAddressBook getContactImage:contact thumbnail:true];
if(image != nil) {
avatarMap[@(ABRecordGetRecordID(contact))] = image;
} else {
avatarMap[@(ABRecordGetRecordID(contact))] = [NSNull null];
}
}
else if(data != [NSNull null]) {
image = data;
}
NSString *name=(NSString *)ABRecordCopyValue(contact, kABPersonFirstNameProperty);
NSString *family=(NSString *)ABRecordCopyValue(contact, kABPersonLastNameProperty);
if(image == nil) {
//image = [UIImage imageNamed"avatar_unknown_small.png"];
RandomColor *random=[[RandomColor alloc] init];
cell.avatarImage.backgroundColor=[random makeColor:[NSString stringWithFormat"%@ %@",name,family]];
cell.imageView.image=nil;
// cell.imageView.backgroundColor=[UIColor whiteColor];
cell.imageLabel.alpha=1;
}
// CFDataRef imageData1 = ABPersonCopyImageData(contact);
// UIImage *image1 = nil;
//
// image1 = [UIImage imageWithDataNSData *)imageData1];
else{
cell.imageLabel.alpha=0;}
cell.imageView.image=nil;
cell.avatarImage.image = image;
cell.contact = contact;
cell.imageView.image=nil;
return cell;
}
- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName"DialerStoryboard" bundle:nil];
ContactProfileViewController *nextview = [storyboard instantiateViewControllerWithIdentifier"rofile"];
OrderedDictionary *subDic = addressBookMap[[addressBookMap keyAtIndex: indexPath.section]];
ABRecordRef lPerson = subDic[[subDic keyAtIndex:indexPath.row]];
// ContactProfileViewController *nextview=[[ContactProfileViewController alloc] init];
nextview.person=lPerson;
[self presentViewController:nextview animated:YES completion:^{
}];
}
第二个UITableView :
-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath{
AddContactTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier"mesal" forIndexPath:indexPath];
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(self.phoneNumbers, indexPath.row);
CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(self.phoneNumbers, indexPath.row);
NSString *phoneNumber = (NSString *)phoneNumberRef;
CFRelease(phoneNumberRef);
CFRelease(locLabel);
cell.numberLabel.text=phoneNumber;
cell.boarderButton.layer.borderColor=cell.boarderButton.titleLabel.textColor.CGColor;
//[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *dest=NULL;;
CFStringRef valueRef = ABMultiValueCopyValueAtIndex(self.phoneNumbers, indexPath.row);
if(valueRef != NULL) {
dest = [ContactProfileViewController localizeLabel:(NSString*) valueRef];
CFRelease(valueRef);
}
if(dest != nil) {
NSLog(@"touch2 cell baby");
NSString *displayName = [FastAddressBook getContactDisplayName:person];
DialerViewController *controller = DYNAMIC_CAST([[PhoneMainView instance] changeCurrentView:[DialerViewController compositeViewDescription]], DialerViewController);
if(controller != nil) {
[controller call:dest displayName:displayName];
}
}
}
SecondTableview(ContactProfileviewcontroller) 的 UIViewController 方法:
-(void)viewDidAppear:(BOOL)animated{
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewDidDisappear:animated];
// [_tableView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.phoneNumbers = ABRecordCopyValue((__bridge ABRecordRef)(self.person), kABPersonPhoneProperty);
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
在 FirstTableviewController 中:
- (void)viewDidLoad {
[super viewDidLoad];
[self changeView:History_All];
// Set selected+over background: IB lack !
[linphoneButton setBackgroundImage:[UIImage imageNamed"buttonR"]
forState:(UIControlStateHighlighted | UIControlStateSelected)];
[linphoneButton setTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey"CFBundleDisplayName"]
forState:UIControlStateNormal];
[LinphoneUtils buttonFixStates:linphoneButton];
// Set selected+over background: IB lack !
[allButton setBackgroundImage:[UIImage imageNamed"buttonch2"]
forState:(UIControlStateHighlighted | UIControlStateSelected)];
[LinphoneUtils buttonFixStates:allButton];
(tableController.tableView).backgroundColor = [UIColor clearColor]; // Can't do it in Xib: issue with ios4
[tableController.tableView setBackgroundView:nil]; // Can't do it in Xib: issue with ios4
}
//——--------
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if(![FastAddressBook isAuthorized]) {
UIAlertView* error = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Address book",nil)
message:NSLocalizedString(@"You must authorize the application to have access to address book.\n"
"Toggle the application in Settings > Privacy > Contacts",nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(@"Continue",nil)
otherButtonTitles:nil];
[error show];
[error release];
//me
// [[PhoneMainView instance] changeCurrentView:[DialerViewController compositeViewDescription]];
// [self presentViewController:error animated:YES completion:nil];
[PhoneMainView.instance popCurrentView];
}
}
Best Answer-推荐答案 strong>
发生这种情况是因为您将相同的数据源绑定(bind)到两个 TableView 。你可以在你的 cellforRow 和 didSelect 中做。检查您正在使用的 tableview 并相应地执行操作。
关于ios - 在 didSelectRowAtIndexPath 执行后 UITableViewCells 消失了吗?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/47449228/
|