2016-06-22 2 views
1

선택한 행이 고유 한 텍스트 색으로 표시된 UIPickerView을 표시하려고합니다. 따라서 선택된 행은 빨간색 글 C로 표시되고 다른 모든 (선택되지 않은) 행은 검은/기본 글 C로 표시됩니다.UIPickerView에서 시작 행의 텍스트 색을 변경하는 방법

내가 이것을 구현 :

완벽하게 선택을 변경할 때 작동하지만 표시되는 초기 행에 대해 아무것도하지 않는
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    UILabel *label = (UILabel*)[pickerView viewForRow:row forComponent:component]; 
    label.textColor = [UIColor redColor]; 

    UILabel *oldLabel = (UILabel*)[pickerView viewForRow:self.previouslySelectedRow forComponent:component]; 
    oldLabel.textColor = [UIColor blackColor]; 

    self.previouslySelectedRow = row; 
} 

. 나는 UIPickerView를 표시

[pickerView selectRow:initialRow inComponent:0 animated:NO]; 

후,하지만 난이 (사용자가 수동으로 선택을 변경하는 경우에만 발생하는 것이) 호출되는 didSelectRow을 초래하지 않는다는 것을 깨달았다 :

내가 부르고있다.

나는 UILabeltextColor 값을 자신을 설정하는 시도 - 무언가 같이 :

UILabel *label = (UILabel*)[pickerView viewForRow:startingRow forComponent:0]; 
    label.textColor = [UIColor redColor]; 

그러나 불행하게도 아무것도 결과가 일어나고있다. pickerview를 표시 한 후에 이것을 호출합니다.

이 작업을 수행하는 쉬운 방법이 있습니까? 내가 놓친 게 있니? 모든/모든 도움에 감사드립니다!

자크

답변

0

는 나 자신이 최근 자 마린 Monotouch (iOS 용 그물) 내가 "깨끗한"수정을 찾을 수 couldnt는에서 작업이 문제를 가지고,하지만 난 지금까지 잘 작동하는 것 같다 아주 작은 해킹을했다. 나는 ObjC를 읽을 수 있기 때문에 C#으로 제 솔루션을 제공 할 것입니다. 그러나 필자는 그것을 작성하려고 노력함으로써 자신을 난처하게 만들 것입니다. 당신은 아이디어를 아주 쉽게 얻을 수 있어야합니다.

  • 재정의 UIPickerViewDelegate. 피커가 처음 사용을 제어 할 때 선택해야하는 데이터 항목의 인덱스를 채울 int 필드를 추가합니다.

    protected class PickerDelegate : UIPickerViewDelegate 
    { 
        // Contruction...overrides etc 
    
        public int SelectIndexOnLoad { get; set; } 
    } 
    
  • 은 이제 pickercontrol의 인스턴스를로드/코드 (있는 viewDidLoad 등) 표시 할 데이터가 포함 된 데이터 소스의 인덱스를 찾을 수의 영역에서/피커에서 첫 번째 선택합니다. viewForRow() 호출 우리가 통과 행 변수에 PickerDelegate.SelectIndexOnLoad과 비교하고, UI 결정 (자 마린 objC 호출의 getView()에 매핑 됨) pickerdelegates에서

    // First get the index value of the current set of data you 
        // want to display, assuming here I have a list of items as my 
        // picker datasource 
    
        var x = this.items.IndexOf(data_value_to_display); 
    
        // Then assign it to our PickerDelegate.SelectIndexOnLoad 
    
        this.myPickerDelegate.SelectIndexOnLoad = x; 
        this.myPickerView.selectRow(x, 0, true); 
    
        // Note in Xamarin, the objc delegate call selectRow() is mapped to Select() 
    
  • public override UIView viewForRow(UIPickerView pickerView, int row, int component, UIView view) 
        { 
         // Forget reusing the passed view 
         // there is a bug in ios 7,8,9 which ensures its ALWAYS null, nice Apple! 
         var view1 = new UILabel(predefined_rect_size); 
         if (this.SelectIndexOnLoad == row) 
         { 
          view1.BackgroundColor = UIColor.Pink; 
         } 
         else 
         { 
          view1.BackgroundColor = UIColor.White; 
         } 
    
        // Optionally reset the SelectIndexOnLoad so that as the user moves 
        // the picker (viewForRow is called constantly for redraws) 
        // it goes back to normal color once moved. 
        // If you want to keep it colored so the user knows the old original value 
         // Simply omit this line 
         this.SelectIndexOnLoad = row; 
    
         return view1; 
    
        } 
    

이 작동하지만, 나는 그것을 좋아하지 않아. Im은 ObjC 전문가가 우리를 깨우칠 것이라고 기대하지만 그때까지는이 해킹이 트릭입니다. 친절한 감사

0

3 가지 경우 'pickerView.selectRow'가

1이라고 확인?

대신 (_ pickerView : UIPickerView, viewForRow 행 : 지능 ..) pickerView 약간의 시간을주고 viewDidLayoutSubviews (선호) 또는 viewDidappear 에 배치의 viewDidLoad 또는 viewWillAppear 에서 호출 할 수 없습니다

하는 모든 돌아갑니다 UIView의

(iphone6과 나중에, 당신은 추가 'self.yourPIckerView.layoutIfNeeded()'행을 선택하기 전에 viewDidLayoutSubviews에 배치하는 경우)

2 didSelectRow 수동으로 호출해야

'pickerView.selectRow'는 didSelectRow를 자동으로 트리거하지 않습니다. 줄이 끝나면 수동으로 트리거하려면이 줄을 배치하십시오. self.pickerView (self.yourPickerView, didSelectRow : 행 inComponent : 광고)

3. 파라미터가 '거짓'이어야 애니메이션

'pickerView.selectRow (행 inComponent : 광고 애니메이션 : FALSE) "

이 '사실'의 경우, 지연에 의해 무시 될 수있는 선택 도구는 결국 수동 및 색상 변경을 선택한 행에 도착까지 시간이 걸릴 것입니다

관련 문제