[英]Get value from a UISlider inside a Cell
我有一個帶有自定義單元格的tableView
,每個單元格都有一個UISlider
。
現在,我想創建一個方法來獲取那些UISliders
的值,但是我不知道如何訪問每個單元格及其滑塊值。
提前致謝!
首先,向每個滑塊添加標簽,這樣就不會出現哪個滑塊:
slider.tag = 0 //Can be any unique integer
然后注冊用於滑塊更改的方法:
[slider addTarget:self action:@selector(sliderUpdate:) forControlEvents:UIControlEventValueChanged];
最后用你的方法
-(void)sliderUpdate:(UISlider *)sender {
int value = sender.value;
int tag = sender.tag;
}
標簽現在將是您之前使用的唯一整數。 這是一種識別元素的方法。
嗯,幾種可能性:
1)保留對UITableViewController中所有單元格的引用
在tableView:cellForRowAtIndexPath:
,就在返回單元格之前,將其添加到作為UITableViewController屬性的數組中
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Set up your cell
[[self allCells] addObject:cell];
return cell;
}
-(void)getSliderValues {
for(CustomCell *cell in [self allCells]){
//Here is your cell
cell;
}
}
2)使用tableView:cellForRowAtIndexPath:
-(void)getSliderValues {
int section = 0;
int numberOfCells = [self tableView:[self tableView] numberOfRowsInSection:section];
for(int row = 0; row < numberOfCells; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
//Here is your cell
CustomCell *cell = [self tableView:[self tableView] cellForRowAtIndexPath:indexPath];
}
}
請注意,在此方法中,如果您正在使用重用標識符,則可能不會獲得想要/需要的單元格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.