如何垂直和水平滚动UI tableview?

时间:2011-03-17 10:15:25

标签: iphone objective-c uitableview uiscrollview scroll

我的应用程序中有表格视图,每行都有长文本,因此可以滚动整个表格,以便用户可以阅读整个文本

我也在使用以下代码来设置我的单元格

-

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

//buttonCount=0;    
static NSString *CellIdentifier = @"Cell";    
static NSUInteger const kLeftLabel = 100;    
static NSUInteger const  kRightLabel = 101;     
row = [indexPath row];      
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
if (cell == nil)       
{    
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];   
CGRect leftFrame = CGRectMake(2, 5, 45, 30);  
CGRect rightFrame = CGRectMake(50, 5, 400, 30);  
left = [[[UILabel alloc]initWithFrame:leftFrame]autorelease];  
left.tag = kLeftLabel;  
left.font = [UIFont systemFontOfSize:13];  
[cell.contentView addSubview:left];  
right = [[[UILabel alloc]initWithFrame:rightFrame]autorelease];  
right.tag=kRightLabel;  
right.font = [UIFont systemFontOfSize:13];  
[cell.contentView addSubview:right];  

}    
else {    
left = (UILabel*)[cell.contentView viewWithTag:kLeftLabel];  
right=(UILabel*)[cell.contentView viewWithTag:kRightLabel];  

}     
left.text =[secondSplitArrayValue objectAtIndex:row];  
right.text=[splitArrayValue objectAtIndex:row];  

if (indexPath.row == 0)   
{  
[cell setSelectionStyle:UITableViewCellSelectionStyleNone ];  

}  

return cell;  
}

通过使用上面的代码,我有两列,所以第一列有小文本,但第二列非常大,阅读我想要水平滚动。

我希望有人能解决这个问题。

提前谢谢

4 个答案:

答案 0 :(得分:1)

要解决您的问题,无需修改表格视图的滚动,您可以通过以下代码来执行此操作...通过使用此代码,您可以在表格视图单元格的默认标签中显示多行中的文本。

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

    static NSString *CellIdentifier = @"MyCell";

    UITableViewCell *cell =(UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = @"Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. ";

    return cell;
}

答案 1 :(得分:0)

您需要对长文本使用多行标签,并通过在下面的函数中使用长文本进行计算来正确设置长单元格的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

了解更多内容,您可以阅读tutorial

答案 2 :(得分:0)

将您的UITableView放在UIScrollview

答案 3 :(得分:0)

但是看看你的情况没有必要做这样的事情,我宁愿根据文字使细胞高度动态。

使用上面的代码可能有助于解决您的问题: 您需要在单元格中使用具有TextView的自定义单元格;

//计算高度&文字宽度

CGSize stringSize = [@“你的样本文本,其高度和宽度需要计算”sizeWithFont:[UIFont boldSystemFontOfSize:[YurTextField font] constrainedToSize:CGSizeMake(230,9999)lineBreakMode:UILineBreakModeWordWrap];

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

static NSString *CellIdentifier = @"MyCell";

UITableViewCell *cell =(UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

 [Over here take TextView calculating the Height & width of String ];
// [add TextView it cells contentView];
 [cell.contentView addSubview:TextView];


return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {

    CGSize stringSize = [@"Your sample text whose height & Width need to be calulated" sizeWithFont:[UIFont           boldSystemFontOfSize:[YurTextField font] constrainedToSize:CGSizeMake(230, 9999) lineBreakMode:UILineBreakModeWordWrap];

         return stringSize.height;

}

如果它无法解决您的目的,请尝试使用以下替代

虽然我可以建议你使用ScrollView&设置代表。

将您的UITAbleView作为SubView添加到ScrollView&根据视图保持滚动视图的大小滚动。

- >查看

- >滚动型

---> UITableView的