2011-03-17 52 views
0

我有表視圖在我的應用程序,每排有長文本,以便它可以滾動整個表,以便用戶可以讀取如何垂直和水平滾動UI tableview?

也是我使用下面的代碼來設置我的手機

整個文本 -

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

通過使用上面的代碼我有兩列所以第一列有小文本,但第二是非常大,閱讀我想滾動水平。

我希望有人能解決這個問題。

預先感謝您

回答

0

您需要使用多標籤長文本,並通過以下功能長文本計算正確設置了長池的高度。

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

更多,你可以閱讀tutorial

+0

是沒可能無多? – Pooja 2011-03-17 10:37:11

+0

我可以知道你爲什麼不想使用? – Jhaliya 2011-03-17 10:42:28

+2

這種方法要好得多,然後讓tableView滾動到兩邊。如果用戶必須在兩個方向上滾動來閱讀整個文本,這是糟糕的UI設計。考慮製作一個顯示整個文本的細節視圖,使其成爲多行或縮小字體大小 – 2011-03-17 10:45:38

0

地方你的UITableView中的UIScrollView

+0

如何爲你的任何好的教程..謝謝你 – Pooja 2011-03-17 10:37:54

+0

UITableView是繼承自UIScrollView,再次將放置你的麻煩。 – Jhaliya 2011-03-17 10:40:05

+0

雅我試圖添加子視圖,但現在我可以滾動,但無法看到我的表格視圖內容 – Pooja 2011-03-17 11:03:13

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

但看你的情況下,沒有必要做這樣的事情,我寧願根據文本,使細胞高度動態。

只需使用上面的代碼可能有助於解決您的問題: 您需要使用單元格中具有TextView的自定義單元格;

//計算高度&寬度文本的

CGSize stringsize的= [@ 「您的示例文本,其高度&寬度需要被計算方法爲」 sizeWithFont:[UIFont boldSystemFontOfSize:[YurTextField字體] 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; 

} 

,如果它不解決,然後你的目的,請嘗試使用下面的替代

雖然我可以建議你使用了滾動&集其委派。

將你的UITAbleView作爲子視圖添加到ScrollView中&根據視圖的大小保持scrollview的多少你想滾動。

- >查看

- >滾動型

--->的UITableView

+0

代碼格式?使用它可以讓每個人更輕鬆。 – visakh7 2011-03-17 11:11:12

+0

有沒有更好的方式來做代碼格式,因爲我是未知的如此.. :( – 2011-03-17 11:17:11

+0

@Ajay,非常感謝你的代碼,將嘗試讓你知道很快 – Pooja 2011-03-17 11:31:52