【问题标题】:iOs create new table cell from a few strings [closed]iOs从几个字符串创建新的表格单元格[关闭]
【发布时间】:2013-01-14 10:33:58
【问题描述】:

我在 SecondViewController 中收到了一些字符串。 在这个视图控制器中,我有一个 tableView,但还没有任何 tableCells。

我收到此信息:

2013-01-14 11:24:22.342 Scanner[16200:c07] (
    123456,
    "Jeans lynn skinny coj glass",
    "119.90",
    "S, M, L, XL",
    "G-Star"
)
2013-01-14 11:24:22.342 Scanner[16200:c07] jsonstring:{"2":"G-Star","3":null,"product":"Jeans lynn skinny coj glass","4":"119.90","maten":"S, M, L, XL","5":"S, M, L, XL","barcode":"123456","afbeelding":null,"0":"123456","winkel":"G-Star","prijs":"119.90","1":"Jeans lynn skinny coj glass"}
2013-01-14 11:24:22.342 Scanner[16200:c07] barcode:123456
2013-01-14 11:24:22.343 Scanner[16200:c07] product:Jeans lynn skinny coj glass
2013-01-14 11:24:22.343 Scanner[16200:c07] prijs:119.90
2013-01-14 11:24:22.343 Scanner[16200:c07] maten:S, M, L, XL
2013-01-14 11:24:22.343 Scanner[16200:c07] winkel:G-Star

从此代码

    array = [[NSArray alloc] init];
    array = a;
    NSLog(@"%@", a);
    string = [[NSString alloc] init];
    string = s;
    NSLog(@"jsonstring:%@", s);
    NSString * barcode = [a objectAtIndex:0];
    NSLog(@"barcode:%@", barcode);
    NSString * product = [a objectAtIndex:1];
    NSLog(@"product:%@", product);
    NSString * prijs = [a objectAtIndex:2];
    NSLog(@"prijs:%@", prijs);
    NSString * maten = [a objectAtIndex:3];
    NSLog(@"maten:%@", maten);
    NSString * winkel = [a objectAtIndex:4];
    NSLog(@"winkel:%@", winkel);

有谁知道如何将它放入一个新单元格中,该单元格的标签为“123456”,旁边有一张图片,其他信息在子视图控制器中?

我愿意在 2 天后悬赏这个问题

【问题讨论】:

  • 我会在 2 天后给出答案。 :p

标签: ios uitableview nsstring nsarray


【解决方案1】:

您可以简单地使用 UITableViewDelegate http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html

使用:

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView(返回你想要在你的tableview中的section数)
  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section(返回特定部分所需的单元格数)

当然还有:

tableView:cellForRowAtIndexPath:

示例:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [array count];

}

还有你的手机:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...

  cell.text = @"lol";


    return cell;
    }

【讨论】:

  • 我应该在 viewcontroller.xib 中创建 tableView 和单元格吗?或者只有表格视图
  • 创建TableView会在实现的时候自动创建cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;方法。
  • 我有一个问题。我插入了你的方法。但是我怎样才能调用应该创建一个新单元格的函数呢?我如何调用函数来创建一个新的单元格
  • 实际上cellFowRowAtIndexPath: 在numberOfRowsInSection:方法不返回0时自动调用。所以如果你想将单元格添加到你的UITableView只需在你的viewcontroller中实现UITableViewDelegate,设置使用:tableview.delegate = self; tableview.dataSource=self; 将数据源委托给您的 uitableview,并在 cellForRowAtIndexPath: 方法中执行以下操作:cell.textLabel.text = [array objectAtIndex:index];
  • 还是不行。我现在拥有的是:
猜你喜欢
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-22
  • 2014-01-13
  • 2013-03-23
  • 1970-01-01
相关资源
最近更新 更多