2013-04-26 74 views
2

在浏览互联网以获取扩展/可折叠UITableViewCells的各种不同方式之后,我在今天早些时候开始了我的方法。扩展UITableViewCell不按预期工作

我有一个数组,保存在我的UITableView(COLLAPSED = 0,EXPANDED = 1)中的每一行的状态。当单击展开按钮时,我将数组的状态颠倒过来,并在需要更新的部分调用reloadSection。

在我的委托方法heightForRowAtIndexPath中,我检查单元格是否应该展开并返回适当的高度。我面临的问题是我的单元格的确确展开了,而不是显示我的内容隐藏在底下(我使用额外的信息使单元格更大,缩短了它在IB中的位置,并启用了剪辑子视图来修复单元格重叠),它显示另外两个与我的背景类似的UITableViewCell组合在一起,并抛出一些标签/按钮的位置关闭。

我包括它看起来像通常和扩大的图片。我已经通过了代码,这一切都看起来没问题,我认为可能有一个设置我需要更改才能正确渲染。

Collapsed

Expanded

Collapsed Cell in IB

Expanded Cell in IB

这里是源:

#define UITABLEVIEWCELLSIZE_COLLAPSED 97 
#define UITABLEVIEWCELLSIZE_EXPANDED 285 
#define EXPANDED 1 
#define COLLAPSED 0 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    NSNumber *state = [cellExpansionArray objectAtIndex:[indexPath section]]; 
    if ([state intValue] == COLLAPSED) { 
     return UITABLEVIEWCELLSIZE_COLLAPSED; 
    }else if([state intValue] == EXPANDED) { 
     return UITABLEVIEWCELLSIZE_EXPANDED; 
    } 
    return UITABLEVIEWCELLSIZE_COLLAPSED; 
} 

//expandCell is called when the arrow indicator is clicked on the cell 
-(IBAction)expandCell:(id)sender 
{ 
    NSInteger cellNumber = [sender tag]; 
    NSNumber *state = [cellExpansionArray objectAtIndex:cellNumber]; 
    if (state.intValue == 0) { 
     [cellExpansionArray replaceObjectAtIndex:cellNumber withObject:[NSNumber numberWithInt:EXPANDED]]; 
    }else if (state.intValue == 1) { 
     [cellExpansionArray replaceObjectAtIndex:cellNumber withObject:[NSNumber numberWithInt:COLLAPSED]]; 
    } 
    //[self.tableView beginUpdates]; 
    //[self.tableView endUpdates]; 
    //[self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:cellNumber] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    [self.tableView reloadData]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"PCFCustomCell"; 
    PCFCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    //cell.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
    [cell setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"1slot.png"]]]; 
    PCFClassModel *course = [classesOffered objectAtIndex:[indexPath section]]; 
    cell.courseCRN.text = [course CRN]; 
    cell.courseDataRange.text = [course dateRange]; 
    cell.courseDaysOffered.text = [course days]; 
    cell.courseHours.text = [course credits]; 
    cell.courseInstructor.text = [course instructor]; 
    cell.courseTime.text = [course time]; 
    cell.courseLocation.text = [course classLocation]; 
    cell.courseSection.text = [course sectionNum]; 
    cell.courseName.text = [course courseNumber]; 
    cell.courseTitle.text = [course classTitle]; 
    cell.courseType.text = [course scheduleType]; 
    //cell.professorEmail.text = [course instructorEmail]; 
    [cell.mailProf setTag:[indexPath section]]; 
    [cell.showCatalog setTag:[indexPath section]]; 
    [cell.followClass setTag:[indexPath section]]; 
    [cell.addToSchedule setTag:[indexPath section]]; 
    [cell.buttonDropDown setTag:[indexPath section]]; 
    [cell.mailProf setHidden:NO]; 
    [cell.mailProf setEnabled:YES]; 
     if (![course instructorEmail]) { 
      [cell.mailProf setTitle:@"NO EMAIL PROVIDED" forState:UIControlStateNormal]; 
      [cell.mailProf setEnabled:NO]; 
     } 
    [cell.followClass setBackgroundImage:[UIImage imageNamed:@"search_res_11.png"] forState:UIControlStateNormal]; 
    [cell.addToSchedule setBackgroundImage:[UIImage imageNamed:@"search_res_09.png"] forState:UIControlStateNormal]; 
    if ([savedResults count] > 0) { 
     for (PCFClassModel *courseTwo in savedResults) { 
      if ([[courseTwo CRN] isEqualToString:[course CRN]]) { 
       [[cell followClass] setBackgroundImage:[UIImage imageNamed:@"favorite_activated.png"] forState:UIControlStateNormal]; 
       break; 
      } 
     } 
    } 
    //schedule 
    if ([savedSchedule count] > 0) { 
     for (PCFClassModel *courseTwo in savedSchedule) { 
      if ([[courseTwo CRN] isEqualToString:[course CRN]]) { 
       [[cell addToSchedule] setBackgroundImage:[UIImage imageNamed:@"purdue_activated.png"] forState:UIControlStateNormal]; 
       break; 
      } 
     } 
    } 
    [cell.mailProf addTarget:self action:@selector(mailProf:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.showCatalog addTarget:self action:@selector(showCatalog:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.followClass addTarget:self action:@selector(followClass:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.addToSchedule addTarget:self action:@selector(addToSchedule:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.buttonDropDown addTarget:self action:@selector(expandCell:) forControlEvents:UIControlEventTouchUpInside]; 
    //get internet data 
    /* 
    [[cell staticAvailable] setHidden:YES]; 
    [[cell available] setHidden:YES]; 
    [cell.available setText:@""]; 
    [[cell remainingActivityIndicator] startAnimating]; 
    dispatch_queue_t getSpots = dispatch_queue_create("GetSpots", nil); 
    dispatch_async(getSpots, ^{ 
     NSString *webData = nil; 
     while (!webData && self.view.window) webData = [PCFWebModel queryServer:[course classLink] connectionType:nil referer:@"https://selfservice.mypurdue.purdue.edu/prod/bwckschd.p_get_crse_unsec" arguements:nil]; 
     if (!self.view.window) return; 
     NSArray *courseRecord = [PCFWebModel parseData:webData type:3]; 
     PCFCourseRecord *record = [courseRecord objectAtIndex:0]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [[cell remainingActivityIndicator] stopAnimating]; 
      [[cell staticAvailable] setHidden:NO]; 
      NSString *val = @"1"; 
      if ([val compare:record.enrolled] > 0) { 
       //[[cell available] setTextColor:[UIColor redColor]]; 
      }else { 
       //[[cell available] setTextColor:[UIColor colorWithRed:.0565442 green:.430819 blue:.0724145 alpha:1]]; 

      } 
      [[cell available] setText:[NSString stringWithFormat:@"SLOTS: %@/%@", record.enrolled,record.capacity]]; 
      [[cell available] setHidden:NO]; 
      [PCFAnimationModel fadeTextIntoView:cell.available time:1]; 
      [PCFAnimationModel fadeTextIntoView:cell.staticAvailable time:1]; 
     }); 
    }); 
    */ 
    // Configure the cell... 
    //[PCFFontFactory convertViewToFont:cell]; 
    [cell.courseTitle setFont:[PCFFontFactory droidSansFontWithSize:13]]; 
    [cell.courseName setFont:[PCFFontFactory droidSansFontWithSize:38]]; 
    [cell.courseDaysOffered setFont:[PCFFontFactory droidSansBoldFontWithSize:11]]; 
    NSNumber *state = [cellExpansionArray objectAtIndex:indexPath.section]; 
    if (state.intValue == COLLAPSED) { 
     [cell.imageViewBackground setHidden:YES]; 
    }else { 
     //[cell.imageViewBackground setHidden:NO]; 
    } 
    return cell; 
} 
+0

检查我的问题:http://stackoverflow.com/q/15245488/1603072 – Bhavin 2013-05-01 14:13:28

+0

这没有帮助,因为我的单元格在按钮水龙头上展开/收缩 – kamran619 2013-05-01 17:43:04

+0

忘记背景图片,所有项目都绑定到顶部和底部,你有没有尝试绑定他们左/顶? – 2013-05-02 07:25:36

回答

0

这看起来像一个autosizing问题给我!您的细胞子视图具有灵活的边距&宽度,高度。我可以提供一些建议,而不是确切的解决方案。希望他们帮助..

  1. 尝试删除自动调整大小属性的子视图(在上查看 - >转到大小XIB点击督察>自动调整大小)或
  2. 创建期间国家扩大使用2点不同细胞的模板和坍方。相同的Cell XIB具有2种不同的单元格视图。使用UINib来加载xib。
  3. 如果图像大小固定,请不要使用图案图像作为单元背景。利用stretchableImageWithLeftCapWidth:topCapHeight图片API。

干杯!
Amar。

+0

我想出了这个问题,我需要让单元格在IB中达到最大尺寸,而不是将其设置为较小的尺寸。由于某种原因,如果我在IB中将其尺寸扩大至最小,如果我在IB中保留它的全部大小,只需更改代码中的高度即可 – kamran619 2013-05-03 17:38:06

-1

您还没有发布您cellForRowAtIndexPath,但我猜你错误地在那里重复使用单元格。请回答一个类似的问题:

Objective C UITableView - Table cells display wrong content after changing cell's height


我试图用类似你这样的代码,它似乎按预期方式工作。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    states = [[NSMutableArray alloc] init]; 
    for (int i = 0; i < 10; i++) 
    { 
     [states addObject:[NSNumber numberWithBool:false]]; 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 10; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"]; 
    cell.textLabel.text = [NSString stringWithFormat:@"Title @%d", indexPath.section]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Subtitle @%d", indexPath.row]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    NSNumber *state = [states objectAtIndex:indexPath.section]; 
    [states replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:!state.boolValue]]; 

    [self.tableView reloadData]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSNumber *state = [states objectAtIndex:indexPath.section]; 
    if (state.boolValue == false) 
    { 
     return 100; 
    } 
    else 
    { 
     return 244; 
    } 
} 
+0

嘿,我不需要改变cellForRowAtIndexPath中的任何内容,因为内容已经在单元格中,不需要改变。它只是在用户点击扩展按钮之前可以看到的。如果你看到我附加的图片,你可以看到我指的是什么。 – kamran619 2013-04-26 02:33:38

+0

所以你基本上有一个单元格原型,当你调用'cellForRowAtIndexPath'时,你完全填充单元格原型?唯一的区别是你正在改变扩展单元的高度? – gberginc 2013-04-26 02:57:11

+0

正确。单元格完全填充在cellForRowAtIndexPath中。唯一做的是高度改变和细胞正在改变,我打电话reloadSections .. 编辑:我将包括cellForRowAtIndexPath反正 – kamran619 2013-04-26 03:02:41

-1

不知道这是否有助于您的情况。但是,如果您想在更改高度后重新加载单元格而不进行重新加载等操作,通常可以执行以下操作。

[tableview beginUpdates]; 
[tableview endUpdates]; 
+0

嘿,我已经尝试了一系列的方式,如你发布的一个,重新加载有问题的部分,并重新加载整个表 - 其中没有任何工作:( – kamran619 2013-04-26 02:32:36

+0

哦,你也做过。对不起,我完全错过了那个位 – Bergasms 2013-04-26 06:19:50