Ios UITableViewCell的多个布局
我正在创建一个使用自定义UITableViewCell类的UITableView。UITableViewCell使用自己的方法根据indexPath和所选索引进行布局 代码如下: AgendaView.h TableViewDelegate方法Ios UITableViewCell的多个布局,ios,uitableview,subclass,Ios,Uitableview,Subclass,我正在创建一个使用自定义UITableViewCell类的UITableView。UITableViewCell使用自己的方法根据indexPath和所选索引进行布局 代码如下: AgendaView.h TableViewDelegate方法 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ EventInfoCell *cell = [t
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
EventInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
if(cell == nil){
cell = [[EventInfoCell alloc]initWithStyle:0 reuseIdentifier:@"infoCell"];
}
cell.frame = CGRectMake(0, 0, 320, [self tableView:tableView heightForRowAtIndexPath:indexPath]);
cell.selectedBackgroundView = nil;
cell.delegate = self;
TimerEvent *event = [_eventsArray objectAtIndex:indexPath.row];
EventInfo *info = [event valueForKey:@"eventInfo"];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"h:mma"];
cell.timeLabel.text = [NSString stringWithFormat:@"%@",[df stringFromDate:event.date]];
cell.titleField.text = [info title];
cell.bgImageView.frame = cell.frame;
cell.countdownLabel.text = [self getCountdown:event];
if (_selectedIndex == indexPath.row){
[cell layoutSelected:YES editing:[tableView isEditing]];
if ([info location])
cell.locationField.text =[NSString stringWithFormat:@"@%@",[info location]];
else
cell.locationField.text = @"Location";
if ([info notes])
cell.notesView.text = [info notes];
else
cell.notesView.text = @"Notes";
}else
[cell layoutSelected:NO editing:NO];
return cell;
}
这是一种可以接受的单元布局方式吗?或者我应该为不同的单元布局创建不同的单元子类吗?如果有帮助的话,当触摸单元格时,它会展开以显示更多信息,这就是为什么我让子类处理它自己的布局
图像:
单元格信息由tableView:cellForRowAtIndexPath期间分配的TimerEvent/Info决定。我认为您应该为每种类型的行创建不同的UITableViewCell子类。这是我可能会如何做的草图:
@interface UITableViewCell (MyTableViewSupport)
@property ( nonatomic, strong ) id value ; // value to display
+(NSString *)identifier ; // reuse identifier for this class
@end
@implementation UITableViewCell (MyTableViewSupport)
+(NSString*)identifier
{
return NSStringFromClass( self ) ;
}
const char * sValueKey = "TableCellValue";
-(void)setValue:(id)value
{
objc_setAssociatedObject( self, sValueKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC ) ;
}
-(id)value
{
return objc_getAssociatedObject( self, sValueKey ) ;
}
@end
@interface TableCellA : TableViewCell
@end
@interface TableCellB : TableViewCell
@end
@implementation TableCellA
-(void)layoutSubviews
{
// one type of layout
}
@end
@implementation TableCellB
-(void)layoutSubviews
{
// another type of layout
}
@end
表视图数据源(可能是视图控制器):
编辑:对上述内容的扩展解释:
UITableViewCell上代码的第一部分(类别)对其进行了扩展,以提供cellIdentifier
类方法和value
属性。现在,UITableViewCell的所有实例都支持设置/获取“value”属性,包括您的子类
比如说,您必须对行类型、员工和公司进行分类。您将创建EmployeeCell
和CompanyCell
:
@interface EmployeeCell : UITableViewCell
-(void)layoutSubviews
{
// layout code for employee cells
}
-(void)setValue:(id)value
{
[ super setValue:value ] ;
// configure the subviews of this employee cell (first name label, last name label, photo, for example) based on `value`
}
@end
@implementation CompanyCell : UITableViewCell
-(void)layoutSubviews
{
// layout code for company cells
}
-(void)setValue:(id)value
{
[ super setValue:value ] ;
// configure the subviews of this company cell (company name label, address label, CEO label, for example) based on `value`
}
@end
您绝对不应该在tableView:cellForRowAtIndexPath方法中分配NSDateFormatter。它太贵了。我应该在其中调用一个方法还是已经有了一个时间数据数组?您可以执行如下操作——对于需要
NSDateFormatter
的单元格,您可以在每个单元格实例上将其作为成员变量分配。。因为您没有在每次出现行时分配新单元格(它们被重用),这应该会有所帮助。事实上,我记得NSDateFormatter
太慢,无法在表单元格中使用,即使是预先分配的。。。我必须记住每一行的日期字符串,以避免在cellForRow内容期间格式化日期。我不确定该值的用途,我添加了不同单元格的图像使用该值表示tableview中由单元格表示的对象。其概念是:单元是某些模型数据的可视化表示。例如,如果您的表视图是通讯簿联系人,您应该设置该单元格显示的通讯簿条目的值。这段代码只是一个示例,说明了如何构建我的单元格/表视图/数据源对象图。。。它不完整。我是这么想的,只是想确认一下。我将尝试为value
添加getter/setter,只是为了展示如何在UITableViewCell
@interface EmployeeCell : UITableViewCell
-(void)layoutSubviews
{
// layout code for employee cells
}
-(void)setValue:(id)value
{
[ super setValue:value ] ;
// configure the subviews of this employee cell (first name label, last name label, photo, for example) based on `value`
}
@end
@implementation CompanyCell : UITableViewCell
-(void)layoutSubviews
{
// layout code for company cells
}
-(void)setValue:(id)value
{
[ super setValue:value ] ;
// configure the subviews of this company cell (company name label, address label, CEO label, for example) based on `value`
}
@end