ios - 可折叠单元格

标签 ios xcode uitableview

我有一个带有两个不同自定义表格单元格的 UITableView。启动应用程序后,第一个单元格显示正常。当您点击它们时,第二个单元格将出现。

任何人都可以帮助我或有想法吗?

非常感谢。

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

    static NSString *MyIdentifier = @"customCell2";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if(cell == nil){
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:9.0];
    }
    return cell;

}

最佳答案

在过去完成自定义 UITableViewCell 后,我通常会在自定义类本身中处理 nib 加载。

自定义单元格的基本标题。

@interface RequestsTableViewCell : UITableViewCell {
    // Ivars.
}
// Properties.
- (id) initWithRequestModel: (RequestModel *) model style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier forQueryType:(int) requestType;
// Other methods, etc.
@end

具有指定初始化程序的自定义单元格。

@implementation RequestsTableViewCell

- (id) initWithRequestModel: (RequestModel *) model style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier forQueryType:(int) requestType {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
       NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"RequestsTableViewCell" owner:self options:nil];
        self = [nibArray objectAtIndex:0];
        requestModel = model;
        queryType = requestType;
        [self setRequestThumbnail];
        [self setRequestCategory];
        [self setRequestAddress];
        [self setRequestStatusDate];
        [self setRequestStatus];
        [self setRequestFollowed];
        [self setRequestComment];
        [self setAppearance];
    }

    return self;
}

自定义 UITableViewCell 也将有一个自定义 xib,它对应并在身份检查器中设置了自定义类。

在 UITableViewController 中。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"Cell Id";
    RequestModel *request = nil;

    // Other code for search, etc

    request = [self.serviceRequests objectAtIndex:indexPath.row];

    RequestsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    if(!cell) {
        cell = [[RequestsTableViewCell alloc] initWithRequestModel:request style:UITableViewCellStyleDefault reuseIdentifier:cellId forQueryType:queryTypeIndicator];
    }

    return cell;
}

听起来您的问题中有不止一种自定义单元格类型?你能详细说明它应该如何运作吗?你说你必须点击一个单元格才能显示另一个单元格,你能解释一下这种交互吗?

关于ios - 可折叠单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29498762/

相关文章:

ios - 从函数 : Unexpectedly found nil while unwrapping an Optional value 设置 UILabel

iphone - 从文件读取数据时出现“NSRangeException”

ios - 为什么我们要在 UIWebView 中模拟触摸?

ios - 选择不调用的联系委托(delegate)方法

iphone - 调整 UIImage 大小以进行编辑并以高分辨率保存?

ios - 为不同屏幕尺寸设置约束的问题

iphone - UITableView dataSource 必须从 tableView :cellForRowAtIndexPath: Exception 返回一个单元格

ios - 在 subview 中与 UITableView 交互

iphone - Nib 中的非全屏表格 View

ios - rvictl 不工作