且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为UITableView创建自定义单元格

更新时间:2022-02-24 22:43:45

您可以做的一件事是在界面构建器中创建自定义UITableViewCell并添加您想要的任何标签等,并在填充行时使用该单元格。

One thing you can do is make a custom UITableViewCell in interface builder and add whatever labels etc you want to it and use that cell when you are populating your rows instead.

在您的.h文件中,您需要在.m文件中使用IBOutlet UITableViewCell yourCellName

in your .h file you would need an IBOutlet UITableViewCell yourCellName

您可以配置您使用以下方法中的自定义单元格:

you can configure your custom cell in a method like the following:

-(void)setUpViewComponents{

        yourCellName.textLabel.text = @"some kind of text";

        yourCellName.textLabel2.text = @"some other text";


        //and so on

}

然后在这个方法中你可以使用自定义单元填充你的表

then in this method you can populate your table with the custom cell(s)

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



        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        }

        cell = yourCellName; //you can make additional 
                             //changes to the cell here if you want


    return cell;
    }

很抱歉这个粗略的例子,希望它至少可以帮助你开始。

Sorry for the rough example, hope it at least helps you get started.