1
我对Objective-C和Xcode很新,希望有人能够给我一些基本的编程问题的帮助。如何将一个NSObject数组绑定到UITableView?
我期待将TranslationRegions(自定义数据模型)添加到堆栈并调用它以在控制器中使用。
数组应该是:
- Loaded在应用程序启动
- 可随时调用,在应用
最终的任何地方,阵列项目将通过加载对Web服务的调用,但现在我想通过代码加载模型中的项目。
我创建了一个带有.h和.m文件的对象(TranslationRegion),我希望用它来绑定表。
我可能会在这里大量错误,所以任何意见将不胜感激。
翻译区域目前只有一个属性descriptionOfRegion。
我已经包含了我的控制器,以显示我正在尝试执行的操作,然后是我的模型的.h和.m文件。
实际控制
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Show Regions"])
{
// Need to instantiate the array in here and pass it to the 'setRegions' within the Table View Controller
//[segue.destinationViewController setRegions:regionArray];
}
}
表视图控制器
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyApp Regions";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the region cell
id region = [self.regions objectAtIndex:indexPath.row];
cell.textLabel.text = [@"" stringByAppendingString:[TranslationRegion descriptionOfRegion:region]];
return cell;
}
TranslationRegion.h
#import <Foundation/Foundation.h>
@interface TranslationRegion : NSObject
@property (nonatomic, readonly) id region;
+ (NSString *)descriptionOfRegion:(id)region;
@end
TranslationRegion.m
#import "TranslationRegion.h"
@interface TranslationRegion()
@property (nonatomic, strong) NSMutableArray *regionStack;
@end
@implementation TranslationRegion
@synthesize regionStack = _regionStack;
- (NSMutableArray *)regionStack
{
if (_regionStack == nil) _regionStack = [[NSMutableArray alloc] init];
return _regionStack;
}
- (id)region
{
return [self.regionStack copy];
}
+ (NSString *)descriptionOfRegion:(id)region
{
return @"This value";
}
@end
谢谢吉姆。为了简洁,我只包含了我的控制器的片段。它不会抛出任何错误。我只是不是100%如何实例化数组并添加一些示例内容。你能给我一个在这里如何做到这一点的例子吗? - if([segue.identifier isEqualToString:@“Show Regions”]) {[segue.destinationViewController setRegions:regionArray]; },其中regionArray包含填充数据的TranslationRegion堆栈。原谅初学者的问题,但我很难过!也许这太迟了.. :) – Nick