0
我想製作一個UITableView,它有很多列,所以整個單元格不能顯示所有數據,第一列顯示標題,這一行不能平滑移動,但其他行可以平滑移動。 我嘗試添加UITableView到UIScrollView,但失敗tableview scrollview
有沒有什麼辦法可以達到這個要求?
我想製作一個UITableView,它有很多列,所以整個單元格不能顯示所有數據,第一列顯示標題,這一行不能平滑移動,但其他行可以平滑移動。 我嘗試添加UITableView到UIScrollView,但失敗tableview scrollview
有沒有什麼辦法可以達到這個要求?
您需要自定義TableViewCell才能正確實現此目的。
編輯: 一個簡單的例子:在實現代碼如下
#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
快速用法:
- (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;
}
你有什麼例子非常感謝你? – Gaojian922188
檢查編輯多列tableviewcells –
快速示例,但我如何解決第一列? – Gaojian922188