배열을 사용하는 것이 가장 좋으므로 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];
}