0

我已经创建了一个包含标签的自定义单元格,但是当我在我的表格视图中添加它而不显示时,我的视图控制器不是TableViewController我已经使用 IB 设置了表格视图并正确设置了它的委托和数据源。

我这样做是:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ExampleAppDataObject* theDataObject = [self theAppDataObject];
    return theDataObject.myAudio.count;
}


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

        static NSString *CellIdentifier = @"Cell";

        ExampleAppDataObject* theDataObject = [self theAppDataObject];

        NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];

        NSArray *parts = [destinationPath componentsSeparatedByString:@"/"];

        NSString *filename = [parts lastObject];

        AudioInfoCell *cell = (AudioInfoCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
        }

        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

        cell.audioName.text = filename;

        return cell;

    }
4

2 回答 2

0

tableView:numberOfRowsInSection首先检查方法中设置了多少行

像下面这样设置自定义单元格后..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AudioInfoCell *cell = (AudioInfoCell *) [tableView dequeueReusableCellWithIdentifier:nil];


    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AudioInfoCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (AudioInfoCell *) currentObject;
                break;

            }
        }
    }

    ExampleAppDataObject* theDataObject = [self theAppDataObject];

    NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];

    NSArray *parts = [destinationPath componentsSeparatedByString:@"/"];

    NSString *filename = [parts lastObject];

    cell.audioName.text = filename;
    return cell;
}
于 2012-12-13T13:19:23.857 回答
0

要检查的两件事:

  1. 确保重用标识符与 Interface Builder 中的标识符匹配
  2. 确保您的numberOfRowsInSectionnumberOfSectionsInTableView 返回正确的号码

另外 - 我不使用:

if (cell == nil) {
        cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}

用于实例化单元格。AudioCell TableViewCell这应该在您实现的子类中得到照顾。我如何实现这种自定义单元的一个例子是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"FlightPickerCell";
    FlightPickerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.tripIDLabel.text = @"Some TripID";
    cell.hospitalLabel.text = @"Some Hospital";
    cell.cityLabel.text = @"Some City";
    return cell;
}
于 2012-12-13T13:21:27.403 回答