Jak*_*ake 2 xcode objective-c uitableview
所以我有一个UITableView,它应循环遍历单个NSMutableArray并将它们中的每一个用作行标签.目前我可以运行的唯一方法是使用0或1行,2或更高的行抛出错误,表示数组索引已关闭.我尝试了NSLog来输出我的数组,并确认它正在读取所有的字符串.
// table methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Set up the cell...
NSString *cellValue = [harvestRecipeList objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
Run Code Online (Sandbox Code Playgroud)
数组代码存储在我在下面添加的完全相同的文件(MasterViewController.m)中.
- (void)viewDidLoad
{
[super viewDidLoad];
harvestRecipeList = [[NSMutableArray alloc] init];
[harvestRecipeList addObject:@"Ice Cream"];
[harvestRecipeList addObject:@"Walnut Cake"];
[harvestRecipeList addObject:@"Cookies"];
[harvestRecipeList addObject:@"Salad"];
[harvestRecipeList addObject:@"Grilled Fish"];
//Set the title
self.navigationItem.title = @"BTN Recipes";
}
Run Code Online (Sandbox Code Playgroud)
我会喜欢这方面的任何帮助,这一直困扰着我.我正在使用[harvestRecipeList count]但这会引发相同的数组索引错误.正如我所提到的,我可以让应用程序以0或1行完美运行 - 提前感谢任何帮助!
编辑:这是我在构建之后在输出窗口中得到的错误:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
Run Code Online (Sandbox Code Playgroud)
EDIT2:包含在我的harvestRecipeList属性设置下面
//MasterViewController.h
@interface MasterViewController : UITableViewController {
NSMutableArray *harvestRecipeList;
}
@property (nonatomic, retain) NSMutableArray *harvestRecipeList;
// and also my MasterViewController.m
@synthesize harvestRecipeList;
Run Code Online (Sandbox Code Playgroud)
EDIT3 这是我为这个项目压缩的源代码.它被称为树屋,现在只是一个测试名称,但你可以在这里使用我的cloudapp.
Updated Solution:
Run Code Online (Sandbox Code Playgroud)
所以我检查了你的代码,发现了问题.请执行下列操作:
Static Cells为Dynamic PrototypesCell标识符字段(这是您用作单元格ID的内容None为Disclosure IndicatortableView:numberOfRowsInSection回报self.harvestRecipeList.count在tableView:cellForRowAtIndexPath:您可以删除以下两行(由故事板提供):
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
将您的Push segue从Master Cell重新创建到Detail View Controller
它应该现在都可以正常工作 - 我已经测试过它的工作原理.基本问题是你已经指定了静态单元而不是动态原型,其余的指令只是在扫描.NSRangeException是由只有一个单元格引起的,因此只显示了所有内容.
希望这可以帮助.
Previous Solution:
Run Code Online (Sandbox Code Playgroud)
那么,一些评论,但是,首先,如果您更新了代码,您可以发布更新吗?
harvestRecipeList添加对象的内容ViewDidLoad似乎与harvestRecipeList您合成的内容不同 - 它将是方法的本地,因此您的实例变量将始终为nil.你应该总是使用self.harvestRecipeList- 到处都这样做.这可以很容易地解释你的NSRangeException.另见下面的#4.tableView:numberOfRowsInSection:,你应该返回self.harvestRecipeList.countcell == nil为dequeueReusableCellWithIdentifier:保证返回非空细胞.您需要检查您是否使用的是iOS4 SDK.@synthesize harvestRecipeList;为,@synthesize harvestRecipeList = _harvestRecipeList;因为这将有助于#1并检查您是否正在访问ivar.尝试#1至少,然后发布您遇到的问题的更新.希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
3543 次 |
| 最近记录: |