我想创建一个UITableView,它有很多列,因此整个单元格无法显示所有数据,第一列显示标题,此行不能平移,但其他行可以平移。 我尝试将UITableView添加到UIScrollView,但失败了
有没有办法达到这个要求?
答案 0 :(得分:0)
您需要制作自定义UITableViewCell才能正确实现此目的。
编辑: 一个简单的例子:
#import <UIKit/UIKit.h>
@interface MultiColumnTableViewCell : UITableViewCell
{
UIButton *leftButton;
UIButton *middleButton;
UIButton *rightButton;
}
@property (nonatomic,retain) UIButton *leftButton;
@property (nonatomic,retain) UIButton *middleButton;
@property (nonatomic,retain) UIButton *rightButton;
@end
#import "MultiColumnTableViewCell.h"
@implementation MultiColumnTableViewCell
@synthesize leftButton;
@synthesize middleButton;
@synthesize rightButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
leftButton = [[UIButton alloc] init];
[leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
leftButton.titleLabel.textAlignment = UITextAlignmentCenter;
leftButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
leftButton.titleLabel.numberOfLines = 2;
middleButton = [[UIButton alloc] init];
[middleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
middleButton.titleLabel.textAlignment = UITextAlignmentCenter;
middleButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
middleButton.titleLabel.numberOfLines = 2;
rightButton = [[UIButton alloc] init];
[rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
rightButton.titleLabel.textAlignment = UITextAlignmentCenter;
rightButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
rightButton.titleLabel.numberOfLines = 2;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
在tableview中快速使用:
- (MultiColumnTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
MultiColumnTableViewCell *cell = (MultiColumnTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MultiColumnTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Set up the cell...
float tableviewWidth = self.myTableView.frame.size.width/3;
int irow = indexPath.row;
NSString *cellValue = myString;
cell.leftButton.frame = CGRectMake(0, 0, tableviewWidth, 64.0);
cell.leftButton.tag = someTag;
[cell.leftButton setTitle:cellValue forState:UIControlStateNormal];
[cell.leftButton addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:cell.leftButton];
cellValue = someOtherString;
cell.middleButton.frame = CGRectMake(tableviewWidth, 0, tableviewWidth, 64.0);
cell.middleButton.tag = someOtherTag;
[cell.middleButton setTitle:cellValue forState:UIControlStateNormal];
[cell.middleButton addTarget:self action:@selector(someOtherAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:cell.middleButton];
cellValue = yetAnotherString
cell.rightButton.frame = CGRectMake(tableviewWidth*2, 0, tableviewWidth, 64.0);
cell.rightButton.tag = yetAnotherTag;
[cell.rightButton setTitle:cellValue forState:UIControlStateNormal];
[cell.rightButton addTarget:self action:@selector(yetAnotherAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:cell.rightButton];
return cell;
}