UIPickerView在发布时返回到第0行

时间:2015-11-01 18:38:44

标签: ios objective-c uipickerview

我向UITableViewCell添加了一个UIPickerView。我正在添加大约5个条目。现在,当我尝试选择一个值时,使用正确的行调用- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component函数。但在scrren上,UIPickerView"滚动"回到第一个元素并且不会粘在选定的值上。

有人有同样的问题吗?

EventSelectCell.h

@class EventSelectCell;

@protocol EventSelectCellDelegate <NSObject>

   - (void)selectedEvent:(Event*)selectedEvent;

@end

@interface EventSelectCell : UITableViewCell <UIPickerViewDataSource, UIPickerViewDelegate>

   @property (strong, nonatomic) id <EventSelectCellDelegate> delegate;

   @property (weak, nonatomic) IBOutlet UIPickerView *selectWheel;

@end

EventSelectCell.m

#import "EventSelectCell.h"

@implementation EventSelectCell
{
    EventManager* eventManager;
    NSArray* listOfEvents;
}

- (void)awakeFromNib {
    self.selectWheel.delegate = self;
    self.selectWheel.dataSource = self;

    // get the EventManger
    eventManager = [EventManager sharedEventManager];

    listOfEvents = [eventManager getListOfEvents];

    [self.selectWheel reloadAllComponents];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {

    return [listOfEvents count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    Event* selectedEvent = [listOfEvents objectAtIndex:row];

    return [selectedEvent name];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    Event* selectedEvent = [listOfEvents objectAtIndex:row];

    [self.delegate selectedEvent:selectedEvent];
}

@end

代表

- (void)selectedEvent:(Event*)selectedEvent {
    [playersInGame removeAllObjects];

    [playersInGame addObjectsFromArray:[self.eventManager getPlayersInEvent:selectedEvent onlyActive:YES]];

    [selectedEvent setIsActive:[NSNumber numberWithInt:1]];

    activeEvent = selectedEvent;

    [self.tableView reloadData];
}

1 个答案:

答案 0 :(得分:-1)

好的,我发现了问题。 UIPickerView位于UITableViewCell中。每当有人使用UIPickerView选择了一些名为[self.tableView reloadData]的东西时,这会导致表重新加载并再次初始化UIPickerView。

现在我只是在桌子上重新加载一个单个部分。