在 didSelectRowAtIndexPath 执行后 UITableViewCells 消失了吗?

Posted

技术标签:

【中文标题】在 didSelectRowAtIndexPath 执行后 UITableViewCells 消失了吗?【英文标题】:UITableViewCells disappear after didSelectRowAtIndexPath perform? 【发布时间】:2017-11-23 06:49:23 【问题描述】:

我有两个表格视图,在第一个 UITableView(联系人表格视图)中加载了所有单元格,当我触摸一个单元格时,它会转到详细联系人,一切都很好。 在第二个UITableView(详细联系人)中,当我触摸单元格(呼叫联系人)和第二个UITableView 方法didSelectRowAtIndexPath 执行时,它可以工作,但会导致第一个UITableView 中的所有数据(联系人)消失。我该如何解决?


附:

(如果我在第二个UITableView 中禁用didSelectRowAtIndexPath,数据将保留在第一个表视图中) (表格视图之间没有segue)


第一UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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 setBackgroundColor:LINPHONE_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 imageWithData:(NSData *)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 imageWithData:(NSData *)imageData1];

    else
        cell.imageLabel.alpha=0;
    cell.imageView.image=nil;
    cell.avatarImage.image = image;


    cell.contact = contact;
    cell.imageView.image=nil;
    return cell;



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"DialerStoryboard" bundle:nil];
    ContactProfileViewController *nextview = [storyboard instantiateViewControllerWithIdentifier:@"Profile"];
    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 *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)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];


    

【问题讨论】:

你的数组在哪里初始化,是在viewdidload还是view会出现在firstTableView中,能否添加firstview的view didload代码 检查您的数据源数组是否有第一个 tableview。 【参考方案1】:

发生这种情况是因为您将相同的数据源绑定到两个表视图。你可以在你的 cellforRow 和 didSelect 中做。检查您正在使用的 tableview 并相应地执行操作。

【讨论】:

以上是关于在 didSelectRowAtIndexPath 执行后 UITableViewCells 消失了吗?的主要内容,如果未能解决你的问题,请参考以下文章

tableView - didSelectRowAtIndexPath 未调用

在 didSelectRowAtIndexPath 中呈现缓慢的 ViewController

我可以麻烦UITableViewDelegate的didSelectRowAtIndexPath:吗?

在 didselectRowAtIndexPath 之后 UITableView 无法滚动底部

在 tableView 上使用 CustomCell,如何调用 didSelectRowAtIndexPath?

didSelectRowAtIndexPath 不会在 ViewController 中触发