2011-12-01 49 views
1

我有一個tableView自定義單元格,每個單元格都有一個UISlider從單元格內的UISlider獲取價值

現在我想創建一個方法來獲取那些UISliders的值,但我不知道如何訪問每個單元格及其滑塊值。

在此先感謝!

回答

6

加上一個標籤給每個滑塊,所以你沒有哪個滑塊是:

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; 
} 

標記現在將成爲您之前使用的同一個唯一整數。這是識別元素的一種方式。

+0

我應該在sliderUpdate內的標籤之間切換嗎?或者只是通過我的13個標籤「IF」? :P – lascort

+0

我實際上會使用循環:'for(int i = 0; i

0

ummmm,幾種可能性:

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]; 
    } 
} 

請注意,在此方法中,如果您使用重用標識符,則可能不會獲取您想要/需要的單元格。