iphone – 如何在编辑模式下将子视图添加到UITableViewCell并调整大小?

编辑:我正在开始一个这样的赏金,因为我正在寻找一个“更清洁”的解决方案与一个UITableViewCell子类,而不是混乱的UITableView委托和数据源。理想情况下,我想看看如何处理UISlider事件。谢谢!

我正在使用一个组风格的UITableView,我有几个像这样的单元格:

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

有人可以指向正确的方向吗?我甚至不确定我是否应该通过将UITableViewCell或IB分类。代码段将高度赞赏:)

首先,创建一个新的基于导航的项目,然后分别创建一个名为CustomCell.h和CustomCell.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

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

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

编译和运行,你有一个完美的版本的控件,你想要的。我甚至执行像黄油一样平滑的动画。玩的开心!

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

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}
http://stackoverflow.com/questions/6137939/how-to-add-a-subview-to-a-uitableviewcell-and-resize-it-while-in-editing-mode

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:iphone – 如何在编辑模式下将子视图添加到UITableViewCell并调整大小?