2011-03-25 13 views
7

मैं उद्देश्य-सी में नया हूं। मेरे पास एक पंक्ति है जिसमें 3 पंक्तियां और 1 खंड है। क्या आप बटन (एडसिटी) दबाकर टेबल में कुछ टेक्स्ट के साथ पंक्ति जोड़ने के लिए मेरी मदद कर सकते हैं?मैं तालिका में पंक्ति कैसे डाल सकता हूं?

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    m_bg.image = [UIImage imageNamed:[WeatherAppDelegate isNight] ? @"SettingsBackNight.png" : @"SettingsBackDay.png" ]; 
    tableView.layer.cornerRadius = 10; 
    m_scrollView.layer.cornerRadius = 10; 
    [m_scrollView setContentSize:CGSizeMake(tableView.frame.size.width, tableView.frame.size.height)]; 
    dataArray = [[NSMutableArray alloc] initWithObjects:@"Moscow", @"London", @"Paris", nil]; 

} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [dataArray count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    [cell.textLabel setText: [dataArray objectAtIndex:indexPath.row]]; 
    return cell; 
} 


-(IBAction)addCity:(id)sender 
{ 
    [dataArray addObject:@"City"]; 
    NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]]; 
    [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop]; 
    [tableView reloadData]; 
} 
+0

मुझे अपना कोड अपडेट किया गया है। यह काम नहीं करता है। – Sveta

+0

मुझे insertRowsAtIndexPaths हटा दिया गया है और मेरा प्रोग्राम काम कर रहा है! – Sveta

उत्तर

13

आपकी तालिका को कुछ स्रोत से अपना डेटा लेना चाहिए जहां आप आवश्यकतानुसार तत्व जोड़ सकते हैं। (कहें, आपके डेटा स्रोत में एनएसएमयूटेबलएरे उदाहरण), तो आपकी विधियां कैसी दिखाई देंगी। सरलता के लिए मान लें कि आपके dataArray युक्त NSStrings है:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [dataArray count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 
    [cell.textLabel setText: [dataArray objectAtIndex:indexPath.row]; 
    return cell; 
} 


-(IBAction)addCity:(id)sender 
{ 
    [tableView beginUpdates]; 
    [dataArray addObject:@"City"]; 
    NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]]; 
    [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop]; 
    [tableView endUpdates]; 
} 
+0

ऐसा कुछ? डेटाअरे = [[एनएसएमयूटेबलएरे ऐलोक] initWithObjects: @ "मॉस्को", @ "लंदन", @ "पेरिस", शून्य]; मेरा कार्यक्रम दुर्घटनाग्रस्त हो गया है। – Sveta

+0

@ स्वेच्छा, हाँ सरणी इस तरह बनाई जानी चाहिए ... क्या आपको अभी भी दुर्घटना है? – Vladimir

+0

नहीं, मुझे insertRowsAtIndexPaths हटा दिया गया है और मेरा प्रोग्राम काम कर रहा है! – Sveta

2

आप सीधे tableView में पंक्तियां सम्मिलित नहीं है। आपने UITableViewDataSource प्रोटोकॉल अपनाया है। आपका डेटा स्रोत टेबल-व्यू ऑब्जेक्ट को उस तालिका के साथ प्रदान करता है जिसे उसे तालिका दृश्य बनाने और संशोधित करने की आवश्यकता होती है। UITableViewDataSource Protocol Reference

इसके अलावा, अपने नमूना कोड को देखते हुए, आपने अपनी तालिका में पंक्तियों की संख्या को 3 तक कोड किया है। इसलिए, UITableView केवल 3 पंक्तियों को प्रदर्शित करने जा रहा है। आपको इस तरह कुछ चाहिए:

// You have a city array you created 
NSMutableArray *cityArray = ...... 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [cityArray count]; 
} 

-(IBAction)addCity:(id)sender 
{ 
    // create a new city object 
    // add the object to your array 
    [cityArray addObject:.... 
    // refresh the table 
    [tableView reloadData]; 
} 
+0

मुझे लगता है कि वह अलग-अलग पंक्तियां डालना चाहता है और एक अच्छी एनीमेशन है, आपकी विधि एक अच्छी एनीमेशन प्रदान नहीं करती है। –

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