2014-12-03 17 views
10

UITableView'im var ve animasyonla altta yeni satırlar eklemek istiyorum, tableView reloadData kullanıyordum ve bu iyi çalışıyordu, ancak animasyon istiyorum. Bu yüzden ben de tableView reloadSections'u değiştirdim ama sorun, 7 satır görebiliyorsam bu satırların 7'sini canlandırıyor. Sadece yeni satırı eklemeyi ve ekrana eklenen her satırı eklenmiş gibi eklemeyi istemiyorum.UITableView, tablonun tamamını yeniden yüklemeden animasyonuyla aşağıya yeni satırlar ekle

Bunu nasıl yapabileceğiniz hakkında bir fikriniz var mı? Eğer bu size yeni hücreyi eklemek ve kullanmak için ne istiyorsunuz animasyon geçmek istiyoruz edildi bölümündeki satırları belirlemenizi sağlar

- (void) insertRowsAtIndexPaths:(NSArray*) indexPaths 
       withRowAnimation: (UITableViewRowAnimation) animation 

bir yöntemini çağırabilir UITableView altında

cevap

11

. örnek kod:

// 
// InsertingNewRowToBottomOfTableViewController.m 
// Testing-Code-Snippets 
// 

#import "InsertingNewRowToBottomOfTableViewController.h" 

@interface InsertingNewRowToBottomOfTableViewController() 
@end 

#define kTestResuseCellIdentifier @"kTestResuseCell" 

@implementation InsertingNewRowToBottomOfTableViewController 
{ 
    NSMutableArray *testArray; 
} 

- (void) viewDidLoad 
{ 
    [super viewDidLoad]; 
    self->testArray = [[NSMutableArray alloc] initWithArray:@[@"Test 1", @"Test 2", @"Test 3"]]; 

    UIRefreshControl *customRefreshControl = [[UIRefreshControl alloc] init]; 
    customRefreshControl.backgroundColor = [UIColor purpleColor]; 
    [customRefreshControl addTarget:self action:@selector(onRefresh:) forControlEvents:UIControlEventValueChanged]; 
    self.refreshControl = customRefreshControl; 
} 

- (void) didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

- (void) onRefresh: (UIRefreshControl*) refreshControl 
{ 
    [refreshControl endRefreshing]; 
    [self->testArray addObject:[NSString stringWithFormat:@"Test %lu", self->testArray.count + 1]]; 
    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self->testArray.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft]; 
    NSLog(@"Added a new cell to the bottom!"); 
} 

#pragma mark - Table view data source 

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

- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section 
{ 
    return self->testArray.count; 
} 


- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTestResuseCellIdentifier forIndexPath:indexPath]; 
    cell.textLabel.text = [self->testArray objectAtIndex:indexPath.row]; 
    return cell; 
} 
@end 

Güncelleme: Swift 3 Versiyon

import UIKit 

class InsertingNewRowToBottomOfTableViewController: UITableViewController 
{ 
    private let testResuseCellIdentifier:String = "kTestResuseCell" 
    private let testArray:NSMutableArray = NSMutableArray(array:["Test 1", "Test 2", "Test 3"]) 

    // MARK: - View Events 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     let refreshControl:UIRefreshControl = UIRefreshControl() 
     refreshControl.backgroundColor = UIColor.purple 
     refreshControl.addTarget(self, action:#selector(self.onRefresh(refreshControl:)), for:.valueChanged) 
     self.refreshControl = refreshControl 
    } 

    override func didReceiveMemoryWarning() 
    { 
     super.didReceiveMemoryWarning() 
    } 

    // MARK: - UIRefreshControl 

    @objc private func onRefresh(refreshControl: UIRefreshControl) 
    { 
     refreshControl.endRefreshing() 
     testArray.add("Test \(self.testArray.count + 1)") 

     let indexPath:IndexPath = IndexPath(row:(self.testArray.count - 1), section:0) 
     self.tableView.insertRows(at:[indexPath], with: .left) 
     NSLog("Added a new cell to the bottom!") 
    } 

    // MARK: - UITableViewDataSource 

    override func numberOfSections(in tableView: UITableView) -> Int 
    { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return self.testArray.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier:testResuseCellIdentifier)! 
     cell.textLabel?.text = self.testArray.object(at: indexPath.row) as? String 
     return cell 
    } 
} 
+0

Yani reloadData ile birlikte kullanan ya da sadece o ki? – Steven

+0

Sadece bu yöntemi çağırabiliyor olmalısınız. Şu anda küçük bir uygulama yapacağım ve bunu yine de iki kere test edeceğim. –

+0

Çok teşekkürler Deniz McDonal. benim günüm – DJtiwari

İlgili konular