如何为UITableView创建自定义单元格 [英] How do I create custom cells for a UITableView

查看:84
本文介绍了如何为UITableView创建自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个带有多个标签的UITableViewCell。我一直在尝试做类似的事情(将名称和电话号码左对齐并在两行上,然后将年龄右对齐在单元格中并可能垂直居中。我尝试了不同的UITableViewCellStyle样式但是想要自定义它,以便单元格可以包含3个标签,而不仅仅是两个。任何帮助将不胜感激。提前感谢!

I am trying to create a UITableViewCell with multiple labels. I have been trying to do something similar (have "Name" and "Phone Number" left aligned and on two lines, then have "Age" right aligned in the cell and possibly vertically centered. I have tried the different UITableViewCellStyle styles but wanted to customize it so that the cell can contain 3 labels instead of just two. Any help would be greatly appreciated. Thanks in advance!

推荐答案

您可以做的一件事是在界面构建器中创建自定义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.

这篇关于如何为UITableView创建自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆