2013-11-22 12 views
6

Tôi có chế độ xem bảng hiển thị các thư tôi tìm nạp từ trên mạng.Chế độ xem bảng cuộn ở giữa khi một hàng mới được chèn

Khi có thư mới, thư sẽ được thêm vào chế độ xem bảng ở dưới cùng.

Khi thư mới xuất hiện, thư sẽ được thêm vào cuối chế độ xem bảng một cách chính xác, nhưng chế độ xem bảng sau đó sẽ cuộn ngay đến giữa. (Vì vậy, nếu bây giờ tôi đã nói 100 ô trong đó, ô 50 sẽ ở giữa chế độ xem cuộn).

Điều kỳ lạ là điều này chỉ xảy ra nếu điểm di chuyển trước khi chèn nằm ở nửa dưới của chế độ xem bảng.

Vì vậy, nếu tôi đang xem ô 10 của 99 và tôi thêm một hàng, nó sẽ được thêm vào dưới cùng tốt.

Nhưng, nếu tôi đang xem ô 75 của 99 và tôi thêm một hàng, vị trí cuộn sẽ nhảy lên lại giữa (~ ô 50) một lần nữa.

Dưới đây là mã của tôi cho thêm một tin nhắn mới vào tableview:

[self.tableView beginUpdates]; 
[self.messages addObject:message]; 

NSArray *indexPaths = @[[NSIndexPath indexPathForRow:(self.messages.count - 1) inSection:0]]; 

[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView endUpdates]; 

Tại sao tableview di chuyển tự động như thế này?

+0

Bạn không muốn di chuyển tableview của bạn ?? –

+0

@hussainShabbir Đó là cuộn mà không có bất kỳ tương tác người dùng nào. Khi tôi thêm một hàng mới, tôi không muốn nó tự động di chuyển đến giữa, mà nó đang làm bây giờ. – Jeff

+0

@ Jeff Bất kỳ may mắn nào khi giải quyết vấn đề này? Tôi có cùng một vấn đề. Sử dụng các tế bào tự kích thước và nó nhảy như điên. –

Trả lời

0

Tôi nghĩ rằng bạn cần phải thay đổi hình ảnh động hàng để none-

[self.tableView 
insertRowsAtIndexPaths:indexPaths 
withRowAnimation: 
UITableViewRowAnimationNone]; 
+0

Tôi đã thử tất cả các loại hoạt ảnh, tất cả đều có cùng kết quả: Bảng xem cuộn lên đến giữa sau khi chèn. – Jeff

0

Hãy thử điều này

[self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

bạn cũng có thể thay đổi vị trí để middile, top hay không.

0

Tôi cũng gặp sự cố này. NSTableView và UITableView có lẽ tương tự ... hành vi cuộn mặc định của chúng, cả SUCK. TableView không được thiết kế để được sử dụng làm trình chỉnh sửa kiểu bảng tính, vì vậy trong những năm qua tôi đã thêm hàng nghìn dòng mã để làm cho nó hoạt động loại như bảng tính. Tôi muốn Apple sẽ chỉ cần tạo một lớp con NSSpreadsheetView của NSTableView để sửa chữa tất cả các công cụ này: - phím mũi tên di chuyển tế bào - sensible cuộn hành vi - thêm hoc lựa chọn ô

cũng muốn xem một lớp con NSLogView của NSTableView mà hoạt động như Console.app, với một trường tìm kiếm.

Giải pháp của tôi là để thêm một số phương pháp loại:

@implementation NSTableView (VNSTableViewCategory) 

-(NSInteger)visibleRow 
{ 
    NSRect theRect = [self visibleRect]; 
    NSRange theRange = [self rowsInRect:theRect]; 
    if (theRange.length == 0) 
     return -1; 
    else if (NSLocationInRange([self editedRow], theRange)) 
     return [self editedRow];   
    else if (NSLocationInRange([self clickedRow], theRange)) 
     return [self clickedRow];  
    else if (NSLocationInRange([self selectedRow], theRange)) 
     return [self selectedRow]; 
    else 
     return theRange.location + (theRange.length/2); 
} 

-(NSRange)visibleRows 
{ 
    NSRect theRect = [self visibleRect]; 
    NSRange theRange = [self rowsInRect:theRect]; 
    return theRange; 
} 

-(void)scrollRowToVisibleTwoThirds:(NSInteger)row 
{ 
    NSRect theVisRect = [self visibleRect]; 
    NSUInteger numRows = [self numberOfRows]; 
    NSRange visibleRows = [self rowsInRect:theVisRect]; 
    //NSUInteger lastVisibleRow = NSMaxRange(visibleRows); 
    if (NSLocationInRange(row, visibleRows)) 
    { 
     if (row - visibleRows.location < (visibleRows.length * 2/3)) 
     { 
     // may need to scroll up 
     if (visibleRows.location == 0) 
      [self scrollRowToVisible:0]; 
     else if ((row - visibleRows.location) > 2) 
      return; 
     } 
    } 

    NSRect theRowRect = [self rectOfRow:row]; 

    NSPoint thePoint = theRowRect.origin; 
    thePoint.y -= theVisRect.size.height/4; // scroll to 25% from top 

    if (thePoint.y < 0) 
     thePoint.y = 0; 

    NSRect theLastRowRect = [self rectOfRow:numRows-1]; 
    if (thePoint.y + theVisRect.size.height > NSMaxY(theLastRowRect)) 
     [self scrollRowToVisible:numRows-1]; 
    else 
    { 
     [self scrollPoint:thePoint]; // seems to be the 'preferred' method of doing this 

     // kpk note: these other approaches cause redraw artifacts in many situations: 
//  NSClipView *clipView = [[self enclosingScrollView] contentView]; 
//  [clipView scrollToPoint:[clipView constrainScrollPoint:thePoint]]; 
//  [[self enclosingScrollView] reflectScrolledClipView:clipView]; 
//  [self scrollRowToVisible:row]; 

//  [[[self enclosingScrollView] contentView] scrollToPoint:thePoint]; 
//  [[self enclosingScrollView] reflectScrolledClipView:[[self enclosingScrollView] contentView]];  
    } 

} 
@end // NSTableView (VNSTableViewCategory) 

Cách sử dụng Ví dụ:

NSRange visRows = [toScenesTable visibleRows]; 
    visRows.length -= 2; 
    if (iAlwaysScroll == YES || !NSLocationInRange(row, visRows)) 
    { 
    [toScenesTable scrollRowToVisibleTwoThirds:row]; // VNSTableViewCategory 
    } 
Các vấn đề liên quan