簡體   English   中英

UITableView-在點擊時獲取數據

[英]UITableView - get data when tapped

我的問題是,當單元被點擊時,是否有可能獲取文本數據並在PHP的ViewController中讀取它?

我猜測didSelectRowAtIndexPath是執行此操作的關鍵,但是我不確定如何編寫。

我能夠從php到我的TableViewController獲取數據,如下所示;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [json count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSDictionary *info = [json objectAtIndex:indexPath.row];
    cell.textLabel.text = [info objectForKey:@"Artist"];
    cell.detailTextLabel.text = [info objectForKey:@"Song"];


    return  cell;
}

我認為您的代碼有誤。 通過檢查來檢查json中是否有任何數據

NSLog(@"%@ and %@",[info objectForKey:@"Artist"],  [info objectForKey:@"Song"]);

告訴我是否有結果 如果沒有,那么您的php中就有一些錯誤,否則您的數據應該顯示在那里。

您編寫的代碼與您一開始所解釋的矛盾。 但是,這是應該怎么做的:

首先,您需要獲取要顯示的數據。 您可以從外部網站(如您所說的php),plist,xml或任何您想要的文件中獲取。 您可以在啟動表視圖時 (例如使用ASIHTTPRequest )或在啟動表視圖並僅使用自定義init方法將其發送之前執行此操作。 其次,您需要閱讀您收到的內容。 通常,很棒的是使用NSArray格式。 這取決於您要從中獲取數據的數據源的實現。

如果您不熟悉Objective-C和可可接觸,則應該從一些有關UITableView的快速教程開始。 我自己寫過一個書,但是目前我猜它(非常)已經過時了。 您仍然可以查看一下: http : //paulpeelen.com/2009/08/14/developing-with-iphone-3-0-x-simple-uitableview/

使用此代碼方法。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
  //retrive your data here.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM