我是IOS開發中的新手。我試圖在UITableView中動態添加行。我在NSMutableArray中添加了新行,並使用與表數據源相同的數組。但是,不知何故,當我在陣列通過按鈕單擊事件添加新的細胞,並嘗試刷新表,前一個單元格disappears.Here是對錶執行的操作順序:UItableview添加新單元格後,前一個單元格消失
我被困在最近幾天,但我無法提供解決方案。
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
您不應該存儲UItableViewCells數組。您應該簡單地將數據(本例中爲字符串)存儲爲數組,然後在'cellForRowAtIndexPath'中返回一個適當配置的單元格。如果您搜索谷歌,有很多表格視圖教程。 – Paulw11
你能提供一個簡單的項目來修復代碼? – BlackM