2014-01-10 26 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