2011-07-25 76 views
2

我有一個具有UISegmentedControl對象的自定義UITableViewCell。我的UITableView(問卷形式)有6個自定義單元格(6個​​分段控件)。我無法獲取段控件的selectedSegmentIndex。我的猜測是單元格在表格生成後被釋放。我使用下面的代碼獲取值:從自定義UITableViewCell獲取值

MyCustomCell *q1 = (MyCustomCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:0]]; 

int segmentIndex = (int) q1.segmentedController.selectedSegmentIndex; 

int segmentIndex總是給人0相同的值不管選擇的指標是什麼。

回答

4

您必須使用正確的方法來初始化索引路徑的部分和行你想使用,否則rowsection屬性將不能正確設置,每一次你會得到相同的細胞:
+(NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;

代碼如下:

// getting the cell at third row of first section 
NSIndexPath *ip = [NSIndexPath indexPathForRow:2 inSection:0]; 
MyCustomCell *q1 = (MyCustomCell *)[tableView cellForRowAtIndexPath:ip]; 
int segmentIndex = (int) q1.segmentedController.selectedSegmentIndex; 

您也可以諮詢:NSIndexPath UIKit Additions Reference以獲取更多信息。

+0

我在第一次使用'indexPathForRow:inSection',但由於某種原因它不工作。 – KDaker

相關問題