添加新单元格时,UItableview上一个单元格将消失

时间:2016-08-12 21:53:04

标签: ios objective-c uitableview

我是IOS开发的新手。我正在尝试在UITableView中动态添加行。我在NSMutableArray中添加新行并使用相同的数组作为表数据源。但是当我通过按钮单击事件在数组中添加新单元格时并尝试刷新表格,之前的单元格消失。这是在表格上执行的一系列操作:

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 个答案:

答案 0 :(得分:1)

你要做的是完全错误的。在您的方法中,您正在制作UITableViewCell,然后将其存储在数据源中。当你真的为iOS做这件事时,你不必将UITableViewCell保留在内存中。

您需要进行以下更改:

  1. 创建一个数据源,它是您的实际数据源,例如数字数组。
  2. 然后在a中使用该数组来显示单元格中的数据。这里重点是确保在此方法中创建单元格。
  3. 现在,由于您的表视图已设置,并且您想要添加更多行,因此请继续前进。假设在单击按钮时要添加一行或多行,您将按如下方式执行:

    1. 您将通过向其添加更多对象来更新数据源,就像在步骤1中我们创建了一个数字数组一样,这里在数组中添加更多数字。
    2. 下一步是创建要插入该数据的索引路径。在我们的例子中,它将是表格的底部。因此,您应该为上一步中在数组中添加的数字创建indexPath。
    3. 最后,现在您的数据源和索引路径数组位于同一页面上,开始表视图更新并插入行。 而已。 :)
    4. 创建了一个虚拟项目来回答。当您运行项目并滚动时,您将看到行是动态添加的,但项目很快,您将能够弄清楚发生了什么。 https://github.com/harsh62/stackoverflow_insert_dynamic_rows