【问题标题】:Passing NSArray to UITable View instead of UILabel [closed]将 NSArray 传递给 UITable View 而不是 UILabel [关闭]
【发布时间】:2013-08-22 20:35:30
【问题描述】:

我有一个已通过NSArray 推送到 Objective-C 的 Javascript 数组。我可以使用以下代码通过UILabel 显示此数组:

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


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

如果我希望它与UITableView 一起工作,而不是UILabel,我该怎么做?

【问题讨论】:

  • 您的意思是您希望每个组件位于单独的单元格中吗?或者你想要一个单元格中的整个字符串?
  • 是的,每个组件都在一个单独的单元格中。所以 UITableView 必须根据我的 Javascript 中数组中的内容来计算要生成多少个单元格,对吧?
  • 好的,这里什么都没有...从数组中您可以决定您的表格需要多少行,并且为每个单元格设置 cell.textLabel.text = testArray[indexpath.row];这应该有效。但除此之外,您还需要了解 tableview 的工作原理。因为如果你知道你就不会问这个问题了。
  • 让我明白这一点——您想使用表格视图,但不想学习如何使用??
  • 不管是 JS 数组还是 Fortran。一旦它进入 NSArray 这就是你所拥有的。如果你知道如何使用表视图,你就会知道如何使用 NSArray 来构建视图(因为 95% 的表视图都是基于 NSArray)。

标签: ios arrays uitableview nsarray


【解决方案1】:

我在草稿文件夹中保存了这篇博文一段时间。你的问题让我记住了并发表了这篇文章:

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

前几天我也在这里回答了一个类似的问题:

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

祝你好运,先生。

【讨论】:

  • 感谢您的实际帮助。
  • 我的荣幸。我们曾经都是菜鸟。我正在尝试更多地了解如何教授一些基本的东西。我可以解释一整天,但无论在另一端是否有意义,这都是困难的部分
【解决方案2】:

A.使您的 testArray 成为 iVar 或属性

B.假设你已经创建了一个 tableView

C.为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;
}

【讨论】:

  • 如何使我的 testArray 成为 iVar 或属性?我真的在网上找不到任何东西。
  • @MinaDawoud - 你不知道如何定义 Objective-C ivar 或属性??
  • 和定义布尔值一样吗?
  • @MinaDawoud 是的,当然你know how to use a table view 但不能定义属性或ivar? :O 你应该先学习语言,然后再学习框架......顺便说一句,网上有很多东西!
猜你喜欢
  • 2020-12-22
  • 2019-03-06
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
  • 2015-10-12
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多