可扩展的UITableViewCell与其他单元格重叠

时间:2014-12-13 14:45:57

标签: ios objective-c uitableview expand

我正在开发一个带有可扩展单元格的UITableView,我使用它:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(_selectedCellIndexPath != nil
   && [_selectedCellIndexPath compare:indexPath] == NSOrderedSame)
    return 160;

return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedCellIndexPath = indexPath;

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

UITableViewCell在Storyboard中设置为:

enter image description here

但是当我改变它时:

enter image description here

添加说明和按钮,它会像这样扰乱所有内容:

enter image description here

即使heightForRowAtIndexPath是40并且它们应该被“隐藏”,如何显示描述textview和更多信息按钮?

或者我采取了错误的做法?

此外,点击和展开不会应用于searchDisplayController.searchResultsTableView。我怎么能这样做?

编辑:cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"IngredientCell";

UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.backgroundColor = [UIColor colorWithRed:1.000 green:1 blue:1 alpha:0.333];
cell.tintColor = [UIColor whiteColor];

UILabel *title = (UILabel*) [cell viewWithTag:1];
UILabel *summary = (UILabel*) [cell viewWithTag:2];
UIButton *moreInfo = (UIButton*) [cell viewWithTag:3];

summary.hidden = YES;
moreInfo.hidden = YES;

[title setTextColor: [UIColor whiteColor]];

// Display recipe in the table cell
if (tableView == self.searchDisplayController.searchResultsTableView) {
    [title setText: [searchResults objectAtIndex:indexPath.row]];
    //set summary
} else {
    [title setText: [ingredients objectAtIndex:indexPath.row]];
    //set summary
}

// Change color if dissallowed
if ([[title text] rangeOfString:@"Egg"].location != NSNotFound) {
    cell.backgroundColor = [UIColor colorWithRed:1.000 green:0.350 blue:0.485 alpha:0.333];
}

// Change color when selected
UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = customColorView;

return cell;
}

0 个答案:

没有答案