处理UITableView方法的对象数组

时间:2014-04-04 15:04:12

标签: ios iphone objective-c uitableview

我已经在线研究过该网站和其他来源,但无法直接找到我要求的内容。我班上有以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

     KJCustomAdTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"CustomerAdCell" bundle:nil] forCellReuseIdentifier:@"myCustomCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCell"];
    }

    //cell.textLabel.text = [_adsObjectArray objectAtIndex:indexPath.row];
    //    AdObject *someAd = _adsObjectArray[0];
    //    cell.titlePl  aceholderLabel.text = someAd.title;
    //    cell.locationPlaceholderLabel.text = someAd.location;
    return cell;
}

当我尝试使用一个字符串数组时,它运行良好,我可以获得一个包含数组中所有项目的表。但是当我尝试使用我的AdObjects时,它并没有起作用。我尝试了一个for循环来遍历数组,但是我知道这个方法被调用来创建每个单元格行,因此导致我的整个表格反映了所有行的相同标题。

我在另一个可以解决此问题的位置寻求您的帮助或教程/问题......

提前致谢!

2 个答案:

答案 0 :(得分:2)

您需要将对象用于当前单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

     KJCustomAdTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"CustomerAdCell" bundle:nil] forCellReuseIdentifier:@"myCustomCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCell"];
    }

    AdObject *someAd = [_adsObjectArray objectAtIndex:indexPath.row];
    cell.titlePlaceholderLabel.text = someAd.title;
    cell.locationPlaceholderLabel.text = someAd.location;
    return cell;
}

此外,您正在使用"细胞重复使用"以错误的方式,至少你应该对相同的细胞类型使用相同的标识符。

答案 1 :(得分:2)

要从阵列中获取标题,您需要执行以下操作:

AdObject *someAd = _adsObjectArray[indexPath.row];
cell.textLabel.text = someAd.title;

而不是:

AdObject *someAd = _adsObjectArray[0];
cell.textLabel.text = someAd.title;
相关问题