【问题标题】:How to scroll UI tableview vertically and horizontally?如何垂直和水平滚动 UI tableview?
【发布时间】:2011-07-17 07:08:40
【问题描述】:

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

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

-

 (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;  
}

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

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

提前谢谢你

【问题讨论】:

    标签: iphone objective-c uitableview uiscrollview scroll


    【解决方案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;
    }
    

    【讨论】:

      【解决方案2】:

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

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

      更多你可以阅读tutorial

      【讨论】:

      • 没有多行就不可能吗?
      • 我可以知道你为什么不想使用吗?
      • 这种方法比让 tableView 滚动到两边要好得多。如果用户必须在两个方向上滚动才能阅读整个文本,这就是糟糕的 UI 设计。考虑制作一个显示整个文本的详细视图,使其成为多行或减小字体大小
      • @Jhaliya,因为我有很多行,而且我正在做我的论文项目,所以我的教授不喜欢它。您知道有时您必须遵循其他想法……如果用户可以在横向视图中查看文本,这可能是个好主意。
      【解决方案3】:

      将你的 UITableView 放在 UIScrollview 中

      【讨论】:

      • UITableView 是继承自 UIScrollView 的,再放就麻烦了。
      • 是的,我尝试添加子视图,但现在我可以滚动但无法看到我的表格视图内容
      • 虽然这可能有效,但 UI 设计和用户体验会很糟糕。 -1
      • @Pooja : 正如我上面提到的“再次放置会给你带来麻烦。”
      【解决方案4】:

      但是看你的情况,没有必要做这样的事情,我更愿意根据文本使单元格高度动态。

      只需使用上面的代码可能有助于解决您的问题: 您需要使用在单元格中具有 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 并将其设置为 Delegate。

      将您的 UITableView 作为 SubView 添加到 ScrollView 并根据您想要滚动的视图保持滚动视图的大小。

      ->查看

      -->滚动视图

      --->UITableView

      【讨论】:

      • 有没有更好的方法来进行代码格式化,因为我对此一无所知.. :(
      • @Ajay,非常感谢你的代码,会尽快让你知道
      猜你喜欢
      • 2014-01-09
      • 1970-01-01
      • 2020-08-17
      • 2015-05-31
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      相关资源
      最近更新 更多