2016-08-12 154 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

相關問題