[英]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.