2011-05-24 10 views
29

मैं डिफ़ॉल्ट रूप से चुनी गई पंक्ति कैसे सेट कर सकता हूं? मैं एक पंक्ति का चयन करना चाहता हूं, indexpath.row के साथ चयनित पंक्ति की संख्या ले लो और फिर तालिका को फिर से लोड करें और चयनित पंक्ति का चयन करें। कृपया क्या कोई मेरी मदद कर सकता है? क्या मुझे एक विधि में चयनित पंक्ति मिल सकती है - (IBAction) segmentedControlIndexChanged? didSelectRowAtIndexPath में नहीं?UITableView में डिफ़ॉल्ट रूप से पुनः लोड करने के बाद चयनित पंक्तियों को कैसे याद रखें और उन्हें बनाए रखें?

यह अगर यह मदद कर सकते हैं समझने के लिए

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    //lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 100, 20) ]; 

    // Configure the cell... 
    NSString *depdateChoosed=[deparatureDates objectAtIndex:depSelectedIndice]; 
    NSString *comeateChoosed=[deparatureDates objectAtIndex:comSelectedIndice]; 
    NSString *choosedDate =[NSString stringWithFormat:@"%@%@",depdateChoosed, comeateChoosed]; 
    NSLog(@"date choisi %@ ",choosedDate); 

    if(segment.selectedSegmentIndex==0) 
    { 
     cell.textLabel.text =[deparatureDates objectAtIndex:indexPath.row];   
    } 
    else if(segment.selectedSegmentIndex==1) { 
     //cell.textLabel.text =[goingBackDates objectAtIndex:indexPath.row]; 
     //cell.textLabel.text=[goingBackDates objectAtIndex:indexPath.row]; 
     cell.textLabel.text =[goingBackDates objectAtIndex:indexPath.row]; 
    } 
    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@" champ séléctionner : %d ",indexPath.row); 

    if(segment.selectedSegmentIndex==0) 
    { 
     depSelectedIndice=indexPath.row; 
    } 
    else if(segment.selectedSegmentIndex==1) { 
     comSelectedIndice=indexPath.row; 
    } 
    //[tableView reloadData]; 
} 

-(IBAction) segmentedControlIndexChanged 
{ 
    int i; 
    switch (self.segment.selectedSegmentIndex) 
    { 
     case 0: 
      i=0; 

      [tableView reloadData]; 
      break; 
     case 1: 
      i=1; 
      NSLog(@"dexieme segment selectionner"); 
      [tableView reloadData]; 
     default: 
      break; 
    } 
} 

उत्तर

64
NSIndexPath *indexPath = [tableView indexPathForSelectedRow]; 

वर्तमान चयनित पंक्ति के लिए आप NSIndexPath दे देंगे मेरी कोड है। इसके बाद आप

[tableView selectRowAtIndexPath:indexPath 
         animated:NO 
       scrollPosition:UITableViewScrollPositionMiddle]; 

उस पंक्ति का चयन करने के लिए बाद में (और स्क्रीन के बीच में दिखाएं) कर सकते हैं।

+0

विधि NSIndexPath indexPath = [tableView indexPathForSelectedRow]; int वापस नहीं कर सकते हैं? – user567

+0

टाइपो, क्षमा करें। यह एक सूचकांक पथ के लिए एक सूचक है। –

+26

साथ ही, सुनिश्चित करें कि आपके 'UITableViewController' पर 'clearsSelectionOnViewWillAppear' को' NO' पर सेट किया गया है, अन्यथा यह दिखाई देने पर पंक्ति को स्वचालित रूप से अचयनित कर देगा। यह मुझे घंटों तक परेशान कर रहा था। – cheeesus

5

इस तरह आप एक UITableView में एक पंक्ति preselect है:

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone]; 

indexPath पंक्ति आपके द्वारा चुने चाहते होने के लिए और उसके बाद के रूप में उपरोक्त विधि कॉल करने के लिए सेट करें।

+0

धन्यवाद लेकिन मैं int का उपयोग नहीं कर सकता? tableView selectRowAtIndexPath: 3? – user567

+0

आप एक पूर्णांक का उपयोग नहीं कर सकते हैं। आपको इंडेक्सपैथ में पास करना होगा, जैसा कि यहां कई उत्तरों में दिखाया गया है। उम्मीद है कि यह किसी की मदद करता है। – Fattie

9

यह यह करना होगा:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone]; 

कहाँ पंक्ति सेल नंबर आप चयन करना चाहते है (0 शुरू होता है ...), और अनुभाग अनुभाग जहां सेल प्रकट होता है।

+0

मेरे पास +130 पंक्तियों के साथ 'UITableView' था और यह मेरे लिए काम करता था। मैं इसे 'viewWillAppear' में बुलाता हूं। +1 धन्यवाद – youssman

30

मैं एक ही सवाल था, और यह है कि कैसे मैं यह किया:

अपने tableViewController में, इस समारोह

(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    if(indexPath.row == 'the row you want to select') 
    { 
     [tableView 
      selectRowAtIndexPath:indexPath 
      animated:TRUE 
      scrollPosition:UITableViewScrollPositionNone 
     ]; 

     [[tableView delegate] 
      tableView:tableView 
      didSelectRowAtIndexPath:indexPath 
     ]; 

    } 

} 
+1

धन्यवाद ... यह मेरे लिए काम करता है – Ronak

+0

हाँ, इस तरह से एक सेल का चयन करते समय, जब इसे तालिका दृश्य में संभाला जा रहा है, यह सुनिश्चित करता है कि यह भी चुना जाएगा यदि आप [reloadData] को कॉल करते हैं। ऐसा लगता है कि इसे [viewWillAppear] में लागू करने से बेहतर काम करता है। धन्यवाद! – pimguilherme

+0

यदि आपका टेबल व्यू एनिमेटेड दिखाई देता है तो यह एकमात्र समाधान है। दृश्य में डिफ़ॉल्ट कक्षों का चयन न करें DidLoad/viewDidAppear! – iOSdev

4

मुझे लगता है कि इस सवाल का नहीं बल्कि पुरानी है (2011) में इस जोड़ने के लिए, लेकिन बस एक अद्यतित उत्तर पोस्ट करने के लिए, अगर कोई (मेरी तरह) इस समस्या की खोज करता है और इस प्रश्न में ठोकर खा जाता है:

आईओएस 9 के साथ, ऐप्पल ने UITableView में बहुत सुधार किया है। सुनिश्चित करें कि फोकस जब एक मेज पुनः लोड है खो नहीं है, सिर्फ इस कार्य करें:

tableView.remembersLastFocusedIndexPath = Yes; 

और यह "जादुई काम" होगा।

और भी बेहतर, UITableViewDelegate में

- (NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView 

लागू तालिका पता कौन सी पंक्ति चयनित करना चाहते हैं जब यह बहुत पहले समय के लिए स्क्रीन तैयार की है यह बताने के लिए।

आईओएस 9 से पहले, मैं आमतौर पर केवल अपना खुद का reloadData विधि लिखता हूं, जो वर्तमान में चयनित पंक्ति प्राप्त करता है, तालिका को फिर से लोड करता है और फिर इसे फिर से चुनता है। या तो UITableViewController में या यदि मुझे उस तालिका के साथ काम करने की आवश्यकता है, जहां मैं सीधे reloadData कोड को कॉल करने से रोक नहीं पा रहा था, तो मैंने UITableView को उपclassed और reloadData ओवरराइड किया (हे, प्रतिनिधिमंडल पसंदीदा तरीका है, लेकिन कभी-कभी आपको केवल उप-वर्ग और ओवरराइड करना पड़ता है, आईओएस में भी)।

0

तालिका दृश्य में चयनित पंक्तियां प्राप्त करने के लिए निम्न कोड का उपयोग करें। :)

NSArray *selectedRows=[tableView indexPathsForSelectedRows]; 
     NSMutableArray *rownumberArray=[[NSMutableArray alloc]init]; 
     for (int i=0; i<selectedRows.count; i++) { 
      NSIndexPath *indexPath = [selectedRows objectAtIndex:i]; 
      NSInteger row = indexPath.row; 
      NSNumber *number = [NSNumber numberWithInteger:row]; 
      [rownumberArray addObject:number]; 
     } 

// rownumberArray में चयनित कक्षों की पंक्ति संख्याएं शामिल हैं।

+0

आप 'के लिए (NSIndexPath * ip चयनित पंक्तियों में) का उपयोग करके इसे थोड़ा सा सरल बना सकते हैं [rowNumberArray addObject: @ (ip.row)]; ' – Koen

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