【发布时间】:2011-12-01 21:36:15
【问题描述】:
我有一个tableView,其中包含自定义单元格,每个单元格都有一个UISlider。
现在我想创建一个方法来获取那些UISliders 的值,但我不知道如何访问每个单元格及其滑块值。
提前致谢!
【问题讨论】:
标签: iphone ios uitableview uislider
我有一个tableView,其中包含自定义单元格,每个单元格都有一个UISlider。
现在我想创建一个方法来获取那些UISliders 的值,但我不知道如何访问每个单元格及其滑块值。
提前致谢!
【问题讨论】:
标签: iphone ios uitableview uislider
首先为每个滑块添加一个标签,以便您不知道哪个滑块是哪个:
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;
}
标签现在将是您之前使用的唯一整数。这是一种识别元素的方式。
【讨论】:
for (int i = 0; i < numberOfCells; i++){ if (i == slider.tag) {//this is the cell that corresponds to this slider}}
嗯,可能性很小:
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];
}
}
请注意,在此方法中,如果您使用重用标识符,您可能不会得到您想要/需要的单元格。
【讨论】: