iphone - 使用数组填充 TableView 的正确方法?

标签 iphone objective-c ios cocoa-touch

我目前正在尝试在我的 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/

相关文章:

ios - 带有 UINavigationController 的 UISplitViewController 作为详细信息 View : push controller via Storyboard segue

iOS - tableview 上的 PopUpViewController 出现在顶部

iphone - ios 模拟器 6.0 中没有照片

ios - 错误的 UILongPressGestureRecognizer IBAction 被解雇

iphone - 另一个速度提升可能吗?

iphone - 排除 iPhone 4S 设备下载应用程序

iOS - TestFlight 是否具有与应用程序实时发布到 App Store 相同的严格标准?

iphone - qLaunchSuccess数据包发送失败,无法在设备中调试

objective-c - 限制多个 UITextField 的长度

ios - 在静音模式下录制和播放音频只需执行一次