扩展UITableViewCell重叠

时间:2015-04-18 14:19:34

标签: ios objective-c uitableview overlapping

我正在尝试创建一个tableView,如果你点击一个单元格,它会展开并显示给按钮。到目前为止我遇到的问题是,如果我用按钮创建Cell然后说它应该只有完整的高度,如果你按它它仍然显示按钮但它们与tableView中的下一个单元重叠

在点击之前,是否存在应隐藏的单元格部分实际上是什么?

这就是我在viewController.m中的内容 你在下面找到的ExpandingCell.h

#import "ViewController.h"
#import "ExpandingCell.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (copy, nonatomic) NSArray *titleArray;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

ExpandingCell *cell;
int selectedIndex = -1;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.titleArray = @[@"Apple", @"Orange", @"Banana", @"Kiwi", @"Melon", @"Strawberry", @"Blueberry", @"Peach"];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.titleArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"expandingCell";

    cell = (ExpandingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    cell.titleLabel.text = self.titleArray[indexPath.row];

    return cell;
}

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

    if (indexPath.row == 0) {
        return 94;
    } else {
        return 54;
    }
}

@end

ExpandCell.h只是Cell上的一些标签和按钮

#import <UIKit/UIKit.h>

@interface ExpandingCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *scoreLabel;
@property (strong, nonatomic) IBOutlet UILabel *backgroundLabel;

@end

仅用于文本目的我已经设置了第一个单元格,好像它是在:“tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath”Method中选择的。

我得到的是一个表格视图,其中第一个单元格完美地显示其高度,然后是其余单元格,其中按钮重叠在下面的下一个单元格上。

(我想展示截图,但它说我需要10个声明才能这样做)

如果是这样,我真的很感谢你的帮助

1 个答案:

答案 0 :(得分:3)

您应该实现tableView:heightForRowAtIndexPath:并返回行的高度,该行取决于您的单元格的状态(是否展开)。然后点按您的手机通话[self.tableView reloadRowsAtIndexPaths:@[indexPathForYourCell]]。要防止重叠,请为单元格的clipsToBounds属性设置YES。

相关问题