将对象添加到数组的开头

时间:2014-09-30 23:33:31

标签: ios uitableview nsmutablearray

我有一个tableView和一个包含填充每个单元格的数据的数组。然后我有一个新视图控制器的按钮来编写一个帖子,然后将它添加到unwindSegue中tableView数组的开头。

postObject *person = [[postObject alloc]initWithName:@"Peter Parker" Event:@"Birthday " Post:@"It's gonna be nuts!!!!" Image:nil profileImage:[UIImage imageNamed:@"guy1.jpeg"]];
postObject *person2 = [[postObject alloc]initWithName:@"Jackie Miller" Event:nil Post:nil Image:[UIImage imageNamed:@"iphone.png"] profileImage:[UIImage imageNamed:@"girl1.jpeg"]];
postObject *person3 = [[postObject alloc]initWithName:@"Johnny" Event:@"Gay fest" Post:@"This app sucks" Image:nil profileImage:[UIImage imageNamed:@"guy2.jpeg"]];
postObject *person4 = [[postObject alloc]initWithName:@"Hailey Walker" Event:@ Beach Fest" Post:@"Hey Guys!!!" Image:nil profileImage:[UIImage imageNamed:@"girl2.jpeg"]];
self.postArray = [[NSMutableArray alloc]initWithObjects:person, person2, person3, person4, nil];

下一页然后将帖子添加到数组中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"newPost"]) {
    NSLog(@"Called");
    postObject *newPost = [[postObject alloc]initWithName:@"Johnny Boy" Event:@"Vegas" Post:@"It works!!"  Image:nil profileImage:nil];
    storyboardController *nextPage = segue.destinationViewController;
    [nextPage.postArray insertObject:newPost atIndex:0];
 }
 }

我在unwindSegue中调用[tableView reloadData],但是tableview完全没有问题。帖子相互重叠,有些文字没有显示。如果我只是将对象添加到数组的末尾,它可以工作,但是当我将它添加到开头时。继承我索引路径上的行的单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
{
postObject *currentPost = [self.postArray objectAtIndex:indexPath.row];
self.cell = (storyboardCell *) [self.tableView dequeueReusableCellWithIdentifier:@"cell"];

if (currentPost.imagePost == nil) {
    _cell.personStringPost.text = currentPost.postString;
    _cell.backgroundColor = [UIColor blueColor];
}
if (currentPost.imagePost != nil) {
    _cell.personImagePost.image = currentPost.imagePost;
    _cell.personStringPost.hidden = true;
    _cell.backgroundColor = [UIColor yellowColor];
}
[self changeToCircle:_cell.profileImage];
_cell.profileImage.image = currentPost.profileImage;

[_cell.nameLabel setTitle:currentPost.nameString forState:UIControlStateNormal];
[_cell.eventLabel setTitle:currentPost.eventString forState:UIControlStateNormal];

[_cell.profileImageButton addTarget:self action:@selector(profilePage) forControlEvents:UIControlEventTouchUpInside];
[_cell.nameLabel addTarget:self action:@selector(profilePage) forControlEvents:UIControlEventTouchUpInside];
[_cell.eventLabel addTarget:self action:@selector(eventPage) forControlEvents:UIControlEventTouchUpInside];
[_cell.likePage addTarget:self action:@selector(likePage) forControlEvents:UIControlEventTouchUpInside];
[_cell.comment addTarget:self action:@selector(commentPage) forControlEvents:UIControlEventTouchUpInside];
    _cell.selectionStyle = UITableViewCellSelectionStyleNone;

return _cell;

}

0 个答案:

没有答案