How to scroll UI tableview vertically and horizont

2019-08-27 03:44发布

问题:

i have table view in my app and each row have long text so its possible to scroll whole table so user can read entire text

also i am using following code to set up my cell

-

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

by use of above code i have two column so 1st column have small text but 2nd is very much big and to read i want to scroll it horizotally.

i hope some one will solve this problem.

thank you in advance

回答1:

To solve your problem there is no need to modify scrolling of table view you can do it by following code... by using this code you can show ur text in multiline in the default label of table view cell.

- (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:

You need to use the multiline label for the long text and set the height of your long cell properly by calculating with long text in below function.

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

for more you can read the tutorial



回答3:

place your UITableView in UIScrollview



回答4:

But looking at your case there is no need to do such thing, I would prefer to make the cells height Dynamic according to the Text.

Just use above code which might help to solve your problem: You need to use Custom Cell having TextView in cell;

//Calculate the Height & Width of Text

CGSize stringSize = [@"Your sample text whose height & Width need to be calulated" 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;

}

if it doesn't solve your purpose then try using below alternative

Although I can suggest you to use the ScrollView & set it Delegate.

Add your UITAbleView as SubView to ScrollView & keep the size of scrollview according to the view how much you want to Scroll.

->View

-->ScrollView

--->UITableView