2010-07-08 37 views
6

पर UISwitch के लिए चयनकर्ता मेरे UITableView में कक्षों के प्रबंधन के लिए TableCellViewController है। प्रत्येक सेल में एक लेबल है UISwitch (dictTypeSwitch)। एक ईवेंट स्विच करने के लिए विधि असाइन करना चाहते हैं ताकि मैं बटन की स्थिति को बचा सकूं।कस्टम कार्रवाई जोड़ें: @VieworViewCell

अब तक मैं इस किया है:

असाइन setState समारोह आपत्ति करने के लिए: घटना से निपटने के लिए

[cell.dictTypeSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventTouchUpInside]; 

समारोह:

- (void)setState:(id)sender { 
    BOOL state = [sender isOn]; 
    NSString *rez = state == YES ? @"YES" : @"NO"; 
    NSLog(rez); 
} 

इस से मैं UISwitch वस्तु मिल जहाँ से मैं राज्य प्राप्त कर सकते हैं

अभी तक सबकुछ ठीक है।

लेकिन अगर मैं यूआईएसविच की स्थिति को सहेजना चाहता हूं, तो मुझे इस सेल के लिए पंक्ति इंडेक्स प्राप्त करना होगा। मैं यह कैसे प्राप्त कर सकते हैं?

The cells are made inside function cellForRowAtIndexPath. Se the code bellow: 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"DictionariesTableCellViewController"; 
    DictionariesTableCellViewController *cell = (DictionariesTableCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 


     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DictionariesTableCellViewController" owner:nil options:nil]; 

     for (id currentObject in topLevelObjects) { 
      if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
       cell = (DictionariesTableCellViewController *) currentObject; 
       break; 
      } 
     } 
    } 

    // Configure the cell... 

    cell.dictTypeLabel.text = [NSString stringWithFormat:@"row - %i",indexPath.row]; 
    [cell.dictTypeSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 

धन्यवाद

उत्तर

4

आप UISwitch के .tag property एक मनमाना पूर्णांक स्टोर करने के लिए उपयोग कर सकते हैं। इसे पंक्ति सूचकांक में सेट किया जा सकता है।

यदि यह UISwitch के माता-पिता से UITableViewCell खोजने के लिए संभव है, आप के साथ

NSIndexPath* indexPath = [theTableView indexPathForCell:theCell]; 
+0

धन्यवाद केनी सूचकांक पथ मिल सकता है ... –

संबंधित मुद्दे