Прежде всего, создайте новый проект на основе навигации, затем создайте новый файл класса с именами 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;
}
person
fscheidl
schedule
02.06.2011