如何在UITableViewCell中添加子视图并在编辑模式下调整其大小?

phi*_*phi 21 iphone objective-c uitableview uislider

编辑:我正在开始这个奖励,因为我正在寻找一个带有UITableViewCell 子类的"更清洁"的解决方案,而不是搞乱UITableView委托和数据源.理想情况下,我想看看我怎么能处理这些UISlider事件.谢谢!

我正在使用组样式UITableView,我有几个看起来像这样的单元格:

在此输入图像描述

现在,当我处于编辑模式时,我想UISlider设置一个正确的UILabel的值,以便结果如下所示:

在此输入图像描述

有人能指出我正确的方向吗?我甚至不确定我是应该通过子类化UITableViewCell还是在IB中做到这一点.代码片段将受到高度赞赏:)

Nil*_*nch 24

作为赏金的傻瓜,我坐下来试验了一下.这是我发现最干净的解决方案:

您将创建一个自定义单元类.正如我从你的帖子中读到的,你对此没有任何问题,我相信它确实是唯一的方法.

在头文件中:

@interface TableCellSlider : UITableViewCell {
    UISlider *cellSlider;
}
Run Code Online (Sandbox Code Playgroud)

在主文件中:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        cellSlider = [[UISlider alloc] initWithFrame:CGRectMake(200, 10, 100, 20)];
        [self addSubview:cellSlider];
        cellSlider.hidden = YES;
    }

    return self;
}

- (void)didTransitionToState:(UITableViewCellStateMask)state
{ 
    if (self.editing) {
        cellSlider.hidden = NO;
    } else {
        cellSlider.hidden = YES;
    }
}

- (float)getValue {
    return cellSlider.value;
}
Run Code Online (Sandbox Code Playgroud)

对于表和单元格高度,将其添加到表脚本:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [self.tableView beginUpdates];
    [super setEditing:editing animated:YES];
    [self.tableView endUpdates];
    //[self.tableView reloadData];
}


    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        if (self.editing) {
            return 50;
        } else {
            return 30;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这应该可以解决问题.您可以在单元格上使用getValue,因此您不必合成滑块,它们将隐藏/重新出现,无论出于何种原因,单元格都可以编辑.

祝你的项目好运:]


fsc*_*idl 12

首先,创建一个新的基于导航的项目,然后创建一个新的名为类文件CustomCell.hCustomCell.m分别.

将此代码复制并粘贴到RootViewController.m文件中:

#import "RootViewController.h"
#import "CustomCell.h"

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem=self.editButtonItem;
}

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

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

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

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.mainLabel.text=@"ValueName";

    return cell;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView.editing){
        return 70;
    }
    return 44;
}

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    return;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)dealloc
{
    [super dealloc];
}

@end
Run Code Online (Sandbox Code Playgroud)

CustomCell.h:

#import <UIKit/UIKit.h>


@interface CustomCell : UITableViewCell {

}

@property (nonatomic, retain) UILabel *mainLabel;
@property (nonatomic, retain) UILabel *detailLabel;
@property (nonatomic, retain) UISlider *slider;

@end
Run Code Online (Sandbox Code Playgroud)

CustomCell.m:

#import "CustomCell.h"

@implementation CustomCell
@synthesize mainLabel, detailLabel, slider;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 12, 280, 0)];
        slider.alpha = 0;
        slider.maximumValue = 30;
        slider.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        [self.contentView addSubview:slider];
        [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventTouchDragInside];
        [slider release];

        mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 12, 150, 20)];
        mainLabel.highlightedTextColor = [UIColor whiteColor];
        mainLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:mainLabel];
        [mainLabel release];

        detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.size.width-180, 12, 150, 20)];
        detailLabel.textAlignment=UITextAlignmentRight;
        detailLabel.text = [NSString stringWithFormat:@"%i", lroundf(slider.value)];
        detailLabel.highlightedTextColor = [UIColor whiteColor];
        detailLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:detailLabel];
        [detailLabel release];
    }
    return self;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationBeginsFromCurrentState:YES];

    if(editing){
        slider.alpha = 1;
        slider.userInteractionEnabled=YES;
    }else{
        slider.alpha = 0;
        slider.userInteractionEnabled=NO;
    }

    [UIView commitAnimations];
}

-(IBAction) sliderChanged:(id) sender{
    detailLabel.text=[NSString stringWithFormat:@"%i", lroundf(slider.value)];
}

- (void)dealloc
{
    [super dealloc];
}

@end
Run Code Online (Sandbox Code Playgroud)

编译并运行,你有一个完美的控件你想要的版本.我甚至实现了像黄油一样光滑的动画.玩得开心!

编辑:如果您确实想使用编辑控件,则不得实现以下方法,并且必须调整标签和滑块的框架.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}
Run Code Online (Sandbox Code Playgroud)