[英]How to update numberOfRowsInSection using NSMutableArray count
在我的應用程序中,我允許用戶將NSStrings的新行添加到UITableView中。 我將這些新字符串保存在NSMutableArray * dataArray中
因此,對於numberOfRowsInSection,我返回[dataArray count],但它似乎一直都返回0,因此我的表不會自動填充。
如果有幫助,當我執行[dataArray count]的NSLog作為添加更多條目時,它將保持為0。為什么它沒有增加?
有什么幫助嗎?
如果不查看@代碼,就無法預測原因。
然后,您也可以嘗試以下HardLuck ...:
首先嘗試NSLog您的dataArray來檢查是否添加了該記錄。
沒關系...您正在獲取記錄。
那么你就錯過了一件事。
重新加載表。[tableView reloadData];
我認為這肯定會幫助您..否則,請在此處放置一些代碼。
您是否初始化了陣列。 這是我的操作方式-更好的方法!
。H:
@interface RootViewController : UITableViewController {
NSArray *array;
}
@property (nonatomic, retain) NSArray *array;
@end
.m:
@implementation RootViewController
@synthesize array;
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *mArry = [[NSMutableArray alloc] init];
[mArry addObject:@"An ObecjT"];
self.array = mArry;
[mArry release];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *aNewArray = [[NSMutableArray alloc] initWithArray:array];
[aNewArray addObject:@"Your Object"];
self.array = aNewArray;
[aNewArray release];
[self.tableView reloadData];
}
- (void)viewDidUnload {
self.array = nil;
}
- (void)dealloc {
[array release];
[super dealloc];
}
您是否在Interface Builder中為UITableView設置了委托? 請檢查一下。
如果已添加,則必須檢查該dataArray是否具有記錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.