-2

尝试使用自定义单元格制作表格视图。我看了一些教程,但没有打开一个新项目,而是一步一步地跟着。我试着用我现在的来做。这样我就可以解决问题并了解更多信息。

所以这里是我到目前为止所做的步骤:简单的表格视图:

在此处输入图像描述

(锻炼列表.xib)

WorkoutList.h(workoutTableView 是你在上图中看到的那个)

在此处输入图像描述

锻炼列表.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.workoutTableView.dataSource = self;
    self.workoutTableView.delegate = self;

    TitleArray = [[NSArray alloc] initWithObjects:@"First", @"Second", @"Third", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return TitleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

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

    WorkoutCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell)
    {
        cell = [[WorkoutCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.workoutTitle.text = [TitleArray objectAtIndex:indexPath.row];
    cell.workoutImage.image = [UIImage imageNamed:@"list-item-icon3"];

    return cell;
}

WorkoutCell.xib:

在此处输入图像描述

自定义类是:WorkoutCell 标识符是:Cell

锻炼细胞.h:

在此处输入图像描述

我所看到的只是一个空的 TableView ..

我知道这是一个很长的问题,但理解它并找出我的错误在哪里对我来说真的很重要。非常感谢 !

4

3 回答 3

1

在您的 viewcontroller.h 文件中,确保您<UITableViewDataSource, UITableViewDelegate>在界面中添加如下:

@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

同样在 .xib 文件中,选择 UITableView 并连接数据源并委托给文件所有者和 tableView 到锻炼表视图,如下面的屏幕截图所示:

在此处输入图像描述

最后在 Workoutecell xib 中,选择文件所有者并将您的视图控制器添加到自定义类。

于 2013-10-31T10:04:41.073 回答
0

这里不需要创建 .h 和 .m 文件..只需创建 WorkoutCell.xib 文件..然后在 File Inspecter(RHS) 中为 UIImageview(tag 1) 和 UILabel(tag2) 设置 Tag 值 ...然后编写以下代码在 cellForRowAtIndexPath 中

        static NSString *MyIdentifier = @"WorkoutCellIdentifier";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

            // If no cell is available, create a new one using the given identifier.
            if (cell == nil) {
                // Use the default cell style.
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WorkoutCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }

            UIImageView *img = (UIImageView *) [cell viewWithTag:1];
            [img setImage:[UIImage imageNamed:@"your image"]];
            UILabel *lbl=(UILabel *)[cell viewWithTag:2];
            [lbl setText:@"your Text"];
            }
     }
于 2013-10-31T10:10:26.437 回答
0

此外,您拥有 TitleArray.count 就好像它是一个属性。count 是数组上的一个方法。您需要调用它,如下所示

return [TitleArray count];
于 2013-10-31T10:12:13.487 回答