我目前正在尝试在我的 iPhone 应用程序上实现一个 TableView ,该应用程序从一个 TableView /类中获取一个数组,并使用该数组填充另一个单独 View 中的表。这是我使用的方法,如果单击/点击数组,则将练习添加到数组中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
NSUInteger *index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
[array insertObject:str atIndex:index];
self.workout = array;
[array release];
}
按下保存按钮后,该数组将存储在我想要在另一个 View 中填充的锻炼数组(数组)中。我是否采取了正确的方法?
这是我的 cellForRowAtIndexPath 方法:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier ];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SectionsTableIdentifier ] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
最佳答案
您可能不想每次都使用新数组重新初始化 self.workout
。
-(void) viewDidLoad
{
self.workout = [[NSMutableArray alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
[self.workout insertObject:str atIndex:0];
}
关于iphone - 使用数组填充 TableView 的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10076394/