使用數組可能是最好的,所以公開一個NSArray
(不可變數組),但在類的實現中,您應該使用NSMutableArray
(可變數組),以便您可以添加/刪除項目但不想類的用戶能夠做到這一點:
.h
文件:
@interface WhatNot : UITableViewController
@property (readonly) NSArray *listOfThings;
...
@end
.m
文件:
@implementation WhatNot()
{
NSMutableArray *_listOfThings;
}
@end
@implementation WhatNot
...
// Override the getter method
- (NSArray *)listOfThings {
return _listOfThings;
}
如果您希望類的用戶能夠將對象添加到數組,然後爲他們提供一個方法,這樣做,並且該方法中,你可以將對象添加到陣列,並安排更新的tableView:
- (void)addThing:(Thing *)thing {
[_listOfThings addObject:thing];
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:[_listOfThings count] - 1];
[self.tableView beginUpdates];
[self.tableView insertRowAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
}