简体   繁体   English

如何使用NSMutableArray计数更新numberOfRowsInSection

[英]How to update numberOfRowsInSection using NSMutableArray count

In my app I allow users to add new rows of NSStrings to a UITableView. 在我的应用程序中,我允许用户将NSStrings的新行添加到UITableView中。 I save these new strings in an NSMutableArray *dataArray 我将这些新字符串保存在NSMutableArray * dataArray中

So for numberOfRowsInSection I return [dataArray count], but it seems to just return 0 all the time so my table is not auto-populating. 因此,对于numberOfRowsInSection,我返回[dataArray count],但它似乎一直都返回0,因此我的表不会自动填充。

If it helps, when I do an NSLog of [dataArray count] as a add more entries, it keeps remaining as 0. Why isn't it increasing? 如果有帮助,当我执行[dataArray count]的NSLog作为添加更多条目时,它将保持为0。为什么它没有增加?

Any help? 有什么帮助吗?

Without looking @ your code ,it's not possible to predict the reason.. 如果不查看@代码,就无法预测原因。

Then also you can try your HardLuck...below: 然后,您也可以尝试以下HardLuck ...:

First try to NSLog your dataArray to check wether the record is adding in that or not. 首先尝试NSLog您的dataArray来检查是否添加了该记录。

It's its ok... you are getting the records. 没关系...您正在获取记录。

then you are missing one thing. 那么你就错过了一件事。

reload the table.. [tableView reloadData]; 重新加载表。[tableView reloadData];

I think this would definitely help you..otherwise put some code over here. 我认为这肯定会帮助您..否则,请在此处放置一些代码。

Have you initialised your Array. 您是否初始化了阵列。 Here is how I've done it - prob a better way! 这是我的操作方式-更好的方法!

.h: 。H:

@interface RootViewController : UITableViewController {
NSArray *array;
}

@property (nonatomic, retain) NSArray *array;

@end

.m: .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];
}

Did you set the delegate for the UITableView in Interface Builder ? 您是否在Interface Builder中为UITableView设置了委托? please check it. 请检查一下。

If you have added, then you have to check with that dataArray whether it is having records or not. 如果已添加,则必须检查该dataArray是否具有记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM