2016-08-12 150 views
0

我是IOS开发中的新手。我试图在UITableView中动态添加行。我在NSMutableArray中添加了新行,并使用与表数据源相同的数组。但是,不知何故,当我在阵列通过按钮单击事件添加新的细胞,并尝试刷新表,前一个单元格disappears.Here是对表执行的操作顺序:UItableview添加新单元格后,前一个单元格消失

step 1

enter image description here

enter image description here

enter image description here

我被困在最近几天,但我无法提供解决方案。

SecondViewController.h

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 

@interface SecondViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate> 
{ 
    AppDelegate *AppDel; 
    CGFloat viewHeight; 


} 
@property (weak, nonatomic) IBOutlet UITextField *typeTextField; 
@property (strong, nonatomic) IBOutlet UIView *mainView; 

@property (weak, nonatomic) IBOutlet UIVisualEffectView *blurback; 
@property (weak, nonatomic) IBOutlet UITableView *tableViewAdd; 
@property (weak, nonatomic) IBOutlet UIView *addView; 
@property (nonatomic, retain) NSArray *myCell; 
@property (weak, nonatomic) IBOutlet UIButton *addTextbutton; 
- (IBAction)addButtonClick:(id)sender; 


@end 

SecondViewController.m

#import "SecondViewController.h" 
#import "AFNetworking.h" 

@interface SecondViewController() <UITableViewDelegate, UITableViewDataSource> 

@end 

@implementation SecondViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    viewHeight=0.0f; 
    AppDel = [[UIApplication sharedApplication] delegate]; 
    NSMutableArray *arr = [[NSMutableArray alloc] init]; 
    if(!self.myCell) 
    { 
    self.myCell = arr; 
    } 
     // Do any additional setup after loading the view, typically from a nib. 
    /* if (!UIAccessibilityIsReduceTransparencyEnabled()) { 
     self.view.backgroundColor = [UIColor clearColor]; 

     UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; 
     UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; 
     blurEffectView.frame = self.view.bounds; 
     blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

     [self.view addSubview:blurEffectView]; 
    } else { 
     self.view.backgroundColor = [UIColor blackColor]; 


    } 
    */ 
    //self.tableViewAdd.delegate=self; 
    //self.tableViewAdd.dataSource=self; 
    self.blurback.frame=self.view.bounds; 
    self.blurback.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
            initWithTarget:self 
            action:@selector(keyboardWillHide:)]; 
    self.typeTextField.delegate=self; 

    [self.blurback addGestureRecognizer:tap]; 


    // [self addadd]; 

    //self.tableViewAdd.frame=CGRectMake(0, 0,self.view.frame.size.width , 
    //self.typeTextField.frame.origin.y-5); 

} 
- (void)keyboardWillShow:(NSNotification *)notification 
{ 
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
    if(viewHeight==0.0f) 
    { 
     viewHeight= self.addView.frame.size.height; 
    } 
    [UIView animateWithDuration:0.3 animations:^{ 
     CGRect f = self.addView.frame; 
     f.size.height = viewHeight-keyboardSize.height; 
     self.addView.frame = f; 
    }]; 
} 
-(void)keyboardWillHide:(NSNotification *)notification 
{ 
    [self.view endEditing:YES]; 
    [UIView animateWithDuration:0.3 animations:^{ 
     CGRect f = self.addView.frame; 
     f.size.height = viewHeight; 
     self.addView.frame = f; 
    }]; 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // return 10; 
    return [self.myCell count]; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    //return cell; 



    return self.myCell[indexPath.row]; 
} 

- (NSInteger)getKeyBoardHeight:(NSNotification *)notification 
{ 
    NSDictionary* keyboardInfo = [notification userInfo]; 
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey]; 
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue]; 
    NSInteger keyboardHeight = keyboardFrameBeginRect.size.height; 
    return keyboardHeight; 
} 
- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (IBAction)addButtonClick:(id)sender 
{ 
    UITableViewCell *cell = [self.tableViewAdd dequeueReusableCellWithIdentifier:@"AddCell"]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AddCell"]; 
     // More initializations if needed. 
    } 
    NSString *messageText =self.typeTextField.text; 
    CGSize boundingSize = CGSizeMake(260-20, 10000000); 
    CGSize itemTextSize = [messageText sizeWithFont:[UIFont systemFontOfSize:14] 
            constrainedToSize:boundingSize 
             lineBreakMode:NSLineBreakByWordWrapping]; 
    float textHeight = itemTextSize.height+7; 
    int x=0; 
    if (textHeight>200) 
    { 
     x=65; 
    }else 
     if (textHeight>150) 
     { 
      x=50; 
     } 
     else if (textHeight>80) 
     { 
      x=30; 
     }else 
      if (textHeight>50) 
      { 
       x=20; 
      }else 
       if (textHeight>30) { 
        x=8; 
       } 
    //[bubbleImage setFrame:CGRectMake(265-itemTextSize.width,5,itemTextSize.width+14,textHeight+4)]; 
    UIImageView *bubbleImage=[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"light-grey-bbl"] stretchableImageWithLeftCapWidth:21 topCapHeight:14]]; 
    [cell.contentView addSubview:bubbleImage]; 
    [bubbleImage setFrame:CGRectMake(50,5, itemTextSize.width+18, textHeight+4)]; 
    // bubbleImage.tag=56; 
    //CGRectMake(260 - itemTextSize.width+5,2,itemTextSize.width+10, textHeight-2)]; 
    UITextView *messageTextview=[[UITextView alloc]initWithFrame:CGRectMake(60,2,itemTextSize.width+10, textHeight-2)]; 
    [cell.contentView addSubview:messageTextview]; 


    messageTextview.editable=NO; 
    messageTextview.text = messageText; 
    messageTextview.dataDetectorTypes=UIDataDetectorTypeAll; 
    messageTextview.textAlignment=NSTextAlignmentJustified; 
    messageTextview.backgroundColor=[UIColor clearColor]; 
    messageTextview.font=[UIFont fontWithName:@"Helvetica Neue" size:12.0]; 
    messageTextview.scrollEnabled=NO; 
    //messageTextview.tag=indexRow; 
    messageTextview.textColor=[UIColor blackColor]; 
    self.myCell= [self.myCell arrayByAddingObject:cell]; 
    NSLog(@"%u",self.myCell.count); 
    [self.tableViewAdd reloadData]; 




} 
@end 
+1

您不应该存储UItableViewCells数组。您应该简单地将数据(本例中为字符串)存储为数组,然后在'cellForRowAtIndexPath'中返回一个适当配置的单元格。如果您搜索谷歌,有很多表格视图教程。 – Paulw11

+0

你能提供一个简单的项目来修复代码? – BlackM

回答

1

你所要做的是完全错误的。在你的方法中,你正在制作一个UITableViewCell,然后将它存储在你的数据源中。当你真的是iOS的工作时,你不必要地将UITableViewCell保存在内存中。

你需要做以下修改:

  1. 使这是实际数据源,例如像数字阵列的数据源。
  2. 然后在cellForRowAtIndex中使用该数组显示单元格中的数据。重要的一点是确保你在这个方法中创建你的单元格。

现在,由于您的表格视图已设置并且您想要添加更多行,请继续前进。假设点击按钮你想添加一行或多行,你可以这样做:

  1. 你会通过添加更多的对象来更新你的数据源,就像在第1步中我们创建了一个数组数组,这里在数组中添加更多的数字。
  2. 下一步将创建您要插入数据的索引路径。在我们的例子中,它将成为表格的底部。所以你应该为你在前一步添加到数组中的数字创建indexPaths。
  3. 最后现在您的数据源和索引路径数组在同一页上,开始表视图更新并插入行。 就是这样。 :)

创建一个虚拟项目来回答。当你运行这个项目并滚动时,你会看到这些行是动态添加的,但是这个项目很快,你可以弄清楚发生了什么。 https://github.com/harsh62/stackoverflow_insert_dynamic_rows

相关问题