UIPickView的基本使用

2021-10-02 17:03:19 字數 3185 閱讀 6832

//uipickview和tableview一樣,想要展示資料也要設定資料來源和**

//設定資料來源

self

.pickview.datasource =

self

;//設定**

self

.pickview.delegate =

self

;//遵守資料來源,**協議:

@inte***ce viewcontroller (

)@property

(weak, nonatomic) iboutlet uipickerview *pickview;

@end

//實現資料來源**方法:

//總共有多少列

-(nsinteger)numberofcomponentsinpickerview:

(uipickerview*

)pickerview

//第component列有多少行.

-(nsinteger)pickerview:

(uipickerview *

)pickerview numberofrowsincomponent:

(nsinteger)component

//返回每一列的寬度

-(cgfloat)pickerview:

(uipickerview *

)pickerview widthforcomponent:

(nsinteger)component

//返回每一行的高度

-(cgfloat)pickerview:

(uipickerview *

)pickerview rowheightforcomponent:

(nsinteger)component

//每一行展示什麼內容

-(nullable nsstring *

)pickerview:

(uipickerview *

)pickerview titleforrow:

(nsinteger)row forcomponent:

(nsinteger)component

//返回每一行的檢視uiview

-(uiview *

)pickerview:

(uipickerview *

)pickerview viewforrow:

(nsinteger)row forcomponent:

(nsinteger)component reusin**iew:

(nullable uiview *

)view

//當前選中的是哪一列的哪一行-(

void

)pickerview:

(uipickerview *

)pickerview didselectrow:

(nsinteger)rowincomponent:

(nsinteger)component

demo

@inte***ce viewcontroller: uiviewcontroller

@end

@inte***ce

viewcontroller()

@property

(nonatomic, weak) iboutlet uilabel *showlabel;

@property

(nonatomic,weak) iboutlet uipickview *pickview;

@property

(nonatomic,strong) nsarray *array;

@end

@implementation viewcontroller-(

void

)viewdidload-(

void

)array

return _array;

}//實現資料來源**方法:

//總共有多少列

-(nsinteger)numberofcomponentsinpickerview:

(uipickerview*

)pickerview

//第component列有多少行.

-(nsinteger)pickerview:

(uipickerview *

)pickerview numberofrowsincomponent:

(nsinteger)component

//返回每一列的寬度

-(cgfloat)pickerview:

(uipickerview *

)pickerview widthforcomponent:

(nsinteger)component

//返回每一行的高度

-(cgfloat)pickerview:

(uipickerview *

)pickerview rowheightforcomponent:

(nsinteger)component

//每一行展示什麼內容

-(nullable nsstring *

)pickerview:

(uipickerview *

)pickerview titleforrow:

(nsinteger)row forcomponent:

(nsinteger)component

//返回每一行的檢視uiview

-(uiview *

)pickerview:

(uipickerview *

)pickerview viewforrow:

(nsinteger)row forcomponent:

(nsinteger)component reusin**iew:

(nullable uiview *

)view

//當前選中的是哪一列的哪一行-(

void

)pickerview:

(uipickerview *

)pickerview didselectrow:

(nsinteger)row incomponent:

(nsinteger)component

@end

UIPickView 基本使用

選擇框 uipickerview pickerview uipickerview alloc initwithframe cgrectmake 0 100 320 216 顯示選中框 pickerview.showsselectionindicator yes pickerview.datasour...

修改UIPickView的高度

發現了乙個問題,uipickerview不能改變小於216的高度,利用setframe方法無法修改uipickerview的高度。解決方法 1 加上pickerview.autoresizingmask uiviewautoresizingflexibleheight uiviewautoresiz...

UIPickView的簡單介紹

設定uipickview的時候,我們主要需要設定一下下面的兩個屬性 uipickerview pickview1 pickview1 uipickerview alloc initwithframe cgrectmake 0,100,300,200 pickview1.delegate self p...