2013-05-14 2 views
2

Я использую UIPickerView в UITableView для выбора целочисленного значения. Я не понимаю, как установить значение метки в конкретной ячейке. Ниже приведен мой код:UIPickerView в UITableViewCell: проблема при отображении значения в определенной ячейке

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell; 
cell = [self.mTicketTable dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
else{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell.bg.png"]]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 

lblType = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 90, 30)]; 
lblPrice = [[UILabel alloc]initWithFrame:CGRectMake(140, 15, 90, 30)]; 
lblSeat = [[UILabel alloc]initWithFrame:CGRectMake(232, 21, 52, 26)]; 
lblSeat.text = strSelected; 
lblSeat.tag = indexPath.row+1; 
lblSeat.textColor = [UIColor lightGrayColor]; 
lblSeat.textAlignment = NSTextAlignmentCenter; 
lblSeat.backgroundColor = [UIColor clearColor]; 
UIButton *btnPicker = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btnPicker setImage:[UIImage imageNamed:@"drop_down.png"] forState:UIControlStateNormal]; 
[btnPicker addTarget:self action:@selector(pickseats:) forControlEvents:UIControlEventTouchUpInside]; 
btnPicker.frame = CGRectMake(232, 20, 80, 30); 

self.mPickerView = [[UIPickerView alloc] init]; 
self.mPickerView.frame = CGRectMake(228, 100, 90, 60); 
self.mPickerView.showsSelectionIndicator = YES; 
self.mPickerView.delegate = self; 
self.mPickerView.dataSource = self; 
[self.mPickerView setNeedsLayout]; 
self.mPickerView.transform = CGAffineTransformMakeScale(0.75f, 0.75f); 
self.mPickerView.hidden = TRUE; 
[self.view addSubview:self.mPickerView]; 

lblType.text = [arrType objectAtIndex:indexPath.row]; 
if([arrCost count] == 0) 
{ 
    lblPrice.text = @"0"; 
} 
else{ 
lblPrice.text = [arrCost objectAtIndex:indexPath.row]; 
} 
[cell addSubview:lblType]; 
[cell addSubview:lblPrice]; 
[cell addSubview:btnPicker]; 
[cell addSubview:lblSeat]; 

return cell; 
} 

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
return 1; 
} 

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
return 10; 
} 

-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
return [numbers objectAtIndex:row]; 
} 

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
strSelected = [numbers objectAtIndex:row]; 
NSLog(@"str selection:%@", strSelected); 
self.mPickerView.hidden = TRUE; 
[self.mTicketTable reloadData]; 
} 

Просьба руководствоваться приведенным выше.

Заранее спасибо.

+0

что это [self.view addSubview: self.mPickerView]; и if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; } else { cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; } –

+0

Вы добавляете pickerview в self.View в cellforrowatIndexpath метод ... который не может работать. –

ответ

2

По мне, что вы можете сделать, это:

Прежде всего присвоить тег вашему btnPicker и lblSeat как

btnPicker.tag = indexPath.row; 
lblSeat.tag = 11111; 

Затем в методе didSelectRow для pickerView присвоить выбранное значение в массив.

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    int index = pickerView.tag; 
    strSelected = [numbers objectAtIndex:row]; 
    NSIndexPath *path = [NSIndexPath indexPathForRow:index inSection:0]; 

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path]; 

    for (UIView* tempView in [cell subviews]) { 
     if ([tempView isKindOfClass:[UILabel class]]) { 
      if (tempView.tag == 11111) { 
       UILabel *lblTemp = (UILabel *)tempView; 
       lblTemp.text = strSelected; 
       break; 
      } 
     } 
    } 

    self.mPickerView.hidden = TRUE; 
} 

Заменить метод pickseats:

-(void)pickseats:(id)sender 
{ 
    UIButton *btn = (UIButton *) sender; 
    self.mPickerView.tag = btn.tag; 
    self.mPickerView.hidden = FALSE; 
} 

Я думаю, что это будет работать. Если не просто дайте мне знать. THNKS.

+0

Это свойство ошибки lblSeat не найдено. –

+0

@iPhone: теперь проверьте это ... я его отредактировал .... в случае пользовательской ячейки то, что я написал ранее, будет работать ..... но теперь попробуем отредактировать один .... –

+0

Существует некоторый индекс, связанный проблема, я думаю, то, как вы делали int index = pickerView.tag, дающий ошибку в строке «UITableViewCell * cell», неявное преобразование в NSIndexPath запрещено ARC. –

Смежные вопросы