行を選択すると pickerView を表示するカスタムセルがあります。次に、値を変更して、detailTextLabel に配置します。ただし、このメソッドは値が変更される前に値を取得します。
//Custom Cell
SimplePickerInputTableViewCell *cell = (SimplePickerInputTableViewCell *) [_tableView dequeueReusableCellWithIdentifier:cellIndentifier2];
if (!cell) {
cell = [[SimplePickerInputTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier2];
UIImageView *lineView2 = [[UIImageView alloc] initWithFrame:CGRectMake(-2, 45, 320, 10)];
lineView2.image = [UIImage imageNamed:@"linha_horizontal.png"];
[cell addSubview:lineView2];
_opcaoTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 200, 20)];
_opcaoTitle.textAlignment = UITextAlignmentLeft;
_opcaoTitle.textColor = [UIColor blackColor];
_opcaoTitle.text = @"Pista - Feminino";
[cell addSubview:_opcaoTitle];
_opcaoValueF = [[UILabel alloc] initWithFrame:CGRectMake(180, 15, 100, 20)];
_opcaoValueF.textColor = [UIColor blackColor];
[cell addSubview:_opcaoValueF];
}
_opcaoValueF.text = [NSString stringWithFormat:@"R$ %@,00", _eventFemininoValue];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
[cell setValue:@"0"];
return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *s = [[[tableView cellForRowAtIndexPath:indexPath] detailTextLabel] text];
NSLog(@"String is: %@", s);
int i = [s intValue];
NSLog(@"This is the int value: %d", i);
}
SimplePicker.m
#import "SimplePickerInputTableViewCell.h"
@implementation SimplePickerInputTableViewCell
@synthesize delegate;
@synthesize value;
__strong NSArray *values = nil;
+ (void)initialize {
values = [NSArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", nil];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.picker.delegate = self;
self.picker.dataSource = self;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
self.picker.delegate = self;
self.picker.dataSource = self;
}
return self;
}
- (void)setValue:(NSString *)v {
value = v;
self.detailTextLabel.text = value;
self.detailTextLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.backgroundColor = [UIColor clearColor];
if (![value isEqualToString:@"0"]) {
[self.picker selectRow:[values indexOfObject:value] inComponent:0 animated:YES];
}
_backgroundValue = [[UIImageView alloc] initWithFrame:CGRectMake(285, 3, 40, 40)];
_backgroundValue.image = [UIImage imageNamed:@"home_icon_popular.png"];
[self addSubview:_backgroundValue];
[self sendSubviewToBack:_backgroundValue];
}
#pragma mark -
#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [values count];
}
#pragma mark -
#pragma mark UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [values objectAtIndex:row];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return 44.0f;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300.0f; //pickerView.bounds.size.width - 20.0f;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.value = [values objectAtIndex:row];
if (delegate && [delegate respondsToSelector:@selector(tableViewCell:didEndEditingWithValue:)]) {
[delegate tableViewCell:self didEndEditingWithValue:self.value];
}
}