10
新行添加到底層我有一個UITableView,我希望能夠將新行添加到底層動畫,我用tableView reloadData
,那是正常的工作,但我想要的動畫。所以我也切換了tableView reloadSections
,但問題是如果我說7行可見,它會動畫所有7行。我只希望它動畫添加新行,而不是屏幕上的每一行,就像它被添加一樣。UITableView的動畫無需重新加載整個表
任何想法如何做到這一點?
新行添加到底層我有一個UITableView,我希望能夠將新行添加到底層動畫,我用tableView reloadData
,那是正常的工作,但我想要的動畫。所以我也切換了tableView reloadSections
,但問題是如果我說7行可見,它會動畫所有7行。我只希望它動畫添加新行,而不是屏幕上的每一行,就像它被添加一樣。UITableView的動畫無需重新加載整個表
任何想法如何做到這一點?
下的UITableView你可以調用一個方法
- (void) insertRowsAtIndexPaths:(NSArray*) indexPaths
withRowAnimation: (UITableViewRowAnimation) animation
這允許您指定部分的行,你想插入一個新的細胞,並通過你想用什麼動畫。 示例代碼:
//
// 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
更新:斯威夫特3版
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
}
}
所以我會使用連同reloadData或只是? – Steven 2014-12-03 01:28:30
你應該可以調用這個方法。我現在將製作一個小應用程序,並且無論如何都要進行雙重測試。 – 2014-12-03 01:29:50
非常感謝海麥當勞。這樣可以節省我的日子 – DJtiwari 2016-04-11 11:15:28