如何用iPad编程实现自定义Table View Cell

如何用iPad编程实现自定义Table View Cell,第1张

UITableViewCell类能够显示出各种各样的风格,但有时候我们需要适应不同的显示模式下的显示。今天的文章中,我们将使用table view去显示一系列自定义的cell。

启动Xcode,选择"Create a new Xcode project",然后选择空应用程序模板,点击Next。命名为 CustomCells,然后照下图那样设置

点击Next,选择项目的存放路径,最后点击Create。

这里需要添加两个文件,UITableViewController以及custom cell对应的xib文件。

Choose File | New >File ,然后添加一个名为 TableViewController 的UITableViewController。

我们以前通常会这样做

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

static NSString *CellIdentiferId = @"MomentsViewControllerCellID"

MomentsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId]

if (cell == nil) {

NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MomentsCell" owner:nil options:nil]

cell = [nibs lastObject]

cell.backgroundColor = [UIColor clearColor]

}

}

return cell

}

严重注意:我们之前这么用都没注意过重用的问题,这样写,如果在xib页面没有设置 重用字符串的话,是不能够被重用的,也就是每次都会重新创建,这是严重浪费内存的,所以,需要修改啊,一种修改方式是使用如下ios5提供的新方式:

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier

还有就是在xib页面设置好(ios5之前也可以用的)

如果忘了在xib中设置,还有一种方式 http://stackoverflow.com/questions/413993/loading-a-reusable-uitableviewcell-from-a-nib

NSArray * nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil]

for (id obj in nibObjects)

{

if ([obj isKindOfClass:[CustomTableCell class]])

{

cell = obj

[cell setValue:cellId forKey:@"reuseIdentifier"]

break

}

}

还有更常用的

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return[self.items count]} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"myCell"] if(!cell){cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"]}cell.textLabel.text =[self.items objectAtIndex:indexPath.row] return cell

}

但是现在有一种新的方式了,可以采用如下的方式

采用registerNib的方式,并且把设置都放在了willDisplayCell方法中了,而不是以前我们经常用的cellForRowAtIndexPath

这个方法我试了下,如果我们在xib中设置了 Identifier,那么此处的必须一致,否则会crash的

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

{

MyCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"]

if (!cell)

{

[tableView registerNib:[UINib nibWithNibName:@"MyCustomCell" bundle:nil] forCellReuseIdentifier:@"myCell"]

cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"]

}

return cell

}

- (void)tableView:(UITableView *)tableView willDisplayCell:(MyCustomCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

cell.leftLabel.text = [self.items objectAtIndex:indexPath.row]

cell.rightLabel.text = [self.items objectAtIndex:indexPath.row]

cell.middleLabel.text = [self.items objectAtIndex:indexPath.row]

}

你可以参考数据库啊,数据库是怎么存储图片的? 以2进制文件存储的,2进制文件的代码可以在数据库里找到,你当然也可以用2进制的方式添加图片。。我也没试过 你可以试试看 死马当活马医。。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/bake/11471408.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存