Iphone 自定义UITableViewCells和以编程方式创建的表

Iphone 自定义UITableViewCells和以编程方式创建的表,iphone,uitableview,Iphone,Uitableview,当我尝试使用自定义单元格将数据从数组加载到表中时,它只显示最后一个单元格,就像覆盖所有单元格一样,直到我到达列表的末尾。该表是以编程方式创建的,并且当单元格不加载空白表滚动时。但是,如果我加载单元格,则只显示最后一个单元格,它不会滚动 我想我打了个错字什么的,但我看不出有什么有用的。解析器工作正常,数组也很好,只是单元的行为很奇怪。我想这样做,使我有一个固定的酒吧,以及表,但首先我需要得到自定义单元格显示在表中,并正确滚动 附表h 我添加了更多代码,问题可能在其他地方 我想你的问题是电池被释放了

当我尝试使用自定义单元格将数据从数组加载到表中时,它只显示最后一个单元格,就像覆盖所有单元格一样,直到我到达列表的末尾。该表是以编程方式创建的,并且当单元格不加载空白表滚动时。但是,如果我加载单元格,则只显示最后一个单元格,它不会滚动

我想我打了个错字什么的,但我看不出有什么有用的。解析器工作正常,数组也很好,只是单元的行为很奇怪。我想这样做,使我有一个固定的酒吧,以及表,但首先我需要得到自定义单元格显示在表中,并正确滚动

附表h
我添加了更多代码,问题可能在其他地方

我想你的问题是电池被释放了。当您调用self.appCell=nil时,对象将被释放,变量设置为nil。尝试更改
cell=appCell
单元格=[[appCell retain]自动释放]以便单元格保留足够长的时间,以便表视图再次保留它


此外,您还可以更改
inteventindex=[indexPath indexaposition:[indexPath length]-1]
to
inteventindex=indexPath.row。行和节属性被添加到NSIndexPath中,专门用于UITableView,并且保证传递到CellForRowatineXpath:的索引路径将恰好包含两个索引。

请尝试以下代码


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier  = @"CellIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell
    if (cell == nil) 
    {
        NSArray *objectList = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id currentObject in objectList) {
            if([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (CustomCell*)currentObject;
                break;
            }
        }
        cell.label.text = [[events objectAtIndex: indexPath.row] objectForKey: @"event"];
    }
    return cell;
}
当您使用NSBundle加载xib文件时,它将返回一个Bundle数组。然后你必须检查它,然后使用它

如果你试着喜欢

NSString *memberName; // member variable

NSString *name = memberName;
memberName = nil;
// the member variable "memberName, giving the reference to name; //So if you set memberName as nil, name also become a nil value. // You have to use like; name = [memberName retain]; memberName = nil; 尝试了解更多关于iPhone开发文档的信息

用于文档。
对于示例代码


我想这会对您有所帮助。

-loadNibNamed:owner:options
返回的单元格已经自动删除;调用
-retain
-autorelease
只会第二次将其添加到autorelease池中,这将一事无成。哎呀,我错过了将单元格加载到可能使用
retain
语义的属性中,在这种情况下,您是对的。嗯,我仍然有同样的问题。好像所有的单元格都不在一张表中,而是叠在一起或者其他什么东西。当我使用自定义单元格时,它看起来是空白的。因为我在一个标签栏应用程序中,所以我可以在标签栏上来回切换,然后只显示一个单元格。如果我使用普通电池,那么它可以正常工作。也许我的单元格设置错误?在interface builder中,我在我的nib中无缘无故地连接了view属性,我将发布一张图片。该方法似乎可行,但我对for循环中的break语句有点担心。但是谢谢你。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier  = @"CellIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell
    if (cell == nil) 
    {
        NSArray *objectList = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id currentObject in objectList) {
            if([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (CustomCell*)currentObject;
                break;
            }
        }
        cell.label.text = [[events objectAtIndex: indexPath.row] objectForKey: @"event"];
    }
    return cell;
}
NSString *memberName; // member variable

NSString *name = memberName;
memberName = nil;
// the member variable "memberName, giving the reference to name; //So if you set memberName as nil, name also become a nil value. // You have to use like; name = [memberName retain]; memberName = nil;