2014-01-10 24 views
0

我目前正在开发一个ios地图应用程序。ios在UItableView中交换UILabel

其中一项功能允许用户获取方向信息。我用2静态单元格创建了一个UItableview。第一个单元显示当前位置和第二个单元显示目的地。这两个单元格都包含一个显示文本的UILabel。

我的问题是我如何让用户交换第一个单元格与第二个,因此他们可以选择从目标位置到当前位置而不是默认方向。

关于如何去做这件事的任何想法。无论如何,我可以拖放UILabel来交换两个标签,或者有​​什么方法可以在单元格之间显示一个按钮,让用户像google map那样进行交换?

谢谢!

回答

0

我会建议去显示交换按钮。所有的按钮将做的是交换你的来源和目的地并重新加载表格。

只是一个快速的书面记录有很多假设这里:

@interface myVC : UIViewController <UITableViewDataSource> 
{ 
    NSString* _source; 
    NSString* _dest; 
} 
@property (nonatomic, weak) IBOutlet UITableView* tableView; 
@end 

然后在执行:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 2; 
} 

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell* cell; 

    if (indexPath.row == 0) 
    { 
    cell = [tableView dequeueReusableCellWithIdentifier:@"SourceCell"]; 
    cell.textLabel.text = source; 
    } 
    else // indexPath.row == 1 
    { 
    cell = [tableView dequeueReusableCellWithIdentifier:@"DestCell"]; 
    cell.textLabel.text = dest; 
    } 

    return cell; 
} 

- (IBAction)swap:(id)sender 
{ 
    NSString* temp = source; 
    source = dest; 
    dest = temp; 
    [self.tableView reloadData]; 
} 

@end