In the previous blog, I used reloaddata to refresh the interface, but there is no animation effect, then I here through the Insertrowsatindexpaths:withrowanimation set animation effect, I hope to help you. Or in the previous code base to fine-tuning, click on the right side of the Barbuttonitem add element, this time I need to add to the _infoarray data source elements are sequentially inserted, instead of adding at the end, which requires the algorithm to sort, the key code is as follows,
?
123456789101112131415161718192021222324252627282930 |
@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>
{
//_infoArray是TableView的数据源
NSMutableArray *_infoArray;
//随机添加的数字都来自_numberArray
NSMutableArray *_numberArray;
}
- (
void
)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//设置导航栏右侧的按钮,并绑定点击事件
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@
"Add"
style:UIBarButtonItemStylePlain target:self action:@selector(addNumber)];
self.navigationItem.rightBarButtonItem = rightItem;
//初始化_numberArray,并且将0~99数字转换成NSNumber,然后加入数组
_numberArray = [[NSMutableArray alloc] initWithCapacity:3];
for
(
int
i = 0;i < 100;i++)
{
NSNumber *tempNumber = [NSNumber numberWithInt:i];
[_numberArray addObject:tempNumber];
}
//初始化数据源数组_infoArray
NSNumber *num0 = [NSNumber numberWithInt:1];
NSNumber *num1 = [NSNumber numberWithInt:30];
NSNumber *num2 = [NSNumber numberWithInt:47];
NSNumber *num3 = [NSNumber numberWithInt:68];
NSNumber *num4 = [NSNumber numberWithInt:75];
NSNumber *num5 = [NSNumber numberWithInt:88];
_infoArray = [[NSMutableArray alloc] initWithObjects:num0,num1,num2,num3,num4,num5, nil];
}
|
Here I initialized the data source _infoarray, where the contents of {1,30,47,68,75,88} digital content, click on the right side of the navigation bar Add button, to the data source in order to insert elements, its binding Addnumber method is as follows,
?
12345678910111213141516171819202122232425262728293031 |
- (
void
)addNumber
{
int count = (
int
)[_numberArray count];
//0~count随机数
int
randomIndex =
rand
()%count;
//随机从_numberArray中获取一个元素insertNumber
NSNumber *insertNumber = [_numberArray objectAtIndex:randomIndex];
int
index = 0;
for
(NSNumber *tempNumber in _infoArray) {
//将要插入的元素与数据源_infoArray中的元素进行大小比较
int
insertInt = [insertNumber intValue];
int
existInt = [tempNumber intValue];
//因为数据源是顺序排列的,所以如果要插入的数字小于_infoArray数据源中的某个值时候则插入数字
//index是用于记录要插入的位置的
if
(insertInt<=existInt) {
[_infoArray insertObject:insertNumber atIndex:index];
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[indexPaths addObject:indexPath];
[self performSelectorOnMainThread:@selector(insertTableViewRow:) withObject:indexPaths waitUntilDone:YES];
//这里是到主线程中刷新界面,因为现在就是在主线程,所以这段话有点多此一举,可以直接调用下面的代码
//[self insertTableViewRow:indexPaths];
//为什么需要一个break语句呢?因为如果不通过break停止for循环,则会出现插入错误。
break
;
}
index++;
}
//为了不重复插入_numberArray中的数据,则要将已经插_infoArray数据源中的数据从原来的数组中删除
[_numberArray removeObject:insertNumber];
}
|
The method above is to insert a new element sequentially, and also to insert a new row in the corresponding number of thetableview rows, insert the row in the main thread by Performselectoronmainthread code, The corresponding-inserttableviewrow method is shown below,
?
12345 |
- ( void )insertTableViewRow:(NSMutableArray *)indexPaths { [self.theTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; //UITableViewRowAnimationFade是一种渐变淡出的效果,这个在删除行的时候使用比较好,这里使用UITableViewRowAnimationAutomatic动画效果比较好 } |
In this way, I add new elements, update the interface, there is the setting of the uitableviewrowanimationautomatic or Uitableviewrowanimationfade gradient animation effect.
iOS deep Learning: (UITableView series 3:insertrow)