2013-08-22 42 views
-5

我有一個Javascript數組,已通過NSArray推送到Objective-C。我能把這個陣列露面通過這個代碼UILabel將NSArray傳遞給UITable視圖而不是UILabel

NSString *testArrayString = [webView stringByEvaluatingJavaScriptFromString:@"testString"]; 
NSArray *testArray = [testArrayString componentsSeparatedByString:@"#"]; 
NSLog(@"%@", testArray); 


self->myLabel.text = [testArray componentsJoinedByString:@","]; 

取而代之的是UILabel,如果我想這與一個UITableView工作,我怎麼會去這樣做呢?

+0

你的意思是你想讓每個組件都在一個單獨的單元格中嗎?或者你想在一個單元格中的整個字符串? – Justin

+0

是的,每個組件都在一個單獨的單元中。所以UITableView將不得不根據我的Javascript中數組中的內容來計算要創建多少個單元格,對不對? –

+0

沒關係,這裏什麼也不做......從數組中你可以決定你的表需要多少行,並且對於每個單元只需設置cell.textLabel.text = testArray [indexpath.row];這應該可行。但除此之外,你需要了解tableview是如何工作的。因爲如果你知道你不會問這個問題。 – koray

回答

2

我將此博客文章保存在我的草稿文件夾中一段時間​​。你的問題讓我記住它,並發表這篇文章:

http://appsylvania.com/post/59049992843/introduction-to-uitableview

這裏我也回答了類似的問題前些天:

https://stackoverflow.com/a/18322362/186911

祝你好運先生。

+0

謝謝你真正的幫助。 –

+1

我的榮幸。我們都是小菜一次。我試圖更多地瞭解如何教授一些基本的東西。我可以整天解釋它,但是不管它在另一端是否有意義,這是困難的部分 – Justin

2

A.讓您的testArray無論是伊娃或財產

B.假設你已經創建了的tableView

UITableView實現以下數據來源:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return testArray.count; //for property use self.testArray.count instead 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = [testArray objectAtIndex:indexPath.row]; 
return cell; 
} 
+0

如何使我的testArray成爲iVar或Property?我在網上找不到任何東西。 –

+0

@MinaDawoud - 你不知道如何定義Objective-C ivar或財產? –

+0

是否與定義布爾值相同? –