UITableView adds blank lines and disables scrolling
Using the fill method of the UITableView from this answer and setting the table to edit mode with:
tableView.editing = true;
And adding the following methods:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
}
I see that the user may accidentally (or intentionally) insert the order descriptor twice and insert a blank line. This will also kill scrolling for the table view. Here's a picture:
There is an empty line between "Logs" and "Settings". I have looped all rows in table view through the answers found here and empty rows are skipped as if they don't exist.
I was able to remove blank rows using [tableView reload] every time the row is moved, which unfortunately leads to incorrect anchoring. However, I was unable to re-enable scrolling. I try to prevent the user from closing and displaying the view.
Does anyone come across this issue and find a suitable job?
EDIT:
Deleting a row doesn't matter - it happens as a result of moving rows around
This is Xcode 6.1 on ios 8/7 - also saw this Xcode 5.x -> ios 7/8. We are focusing on ipads, so not sure if this is a problem on iphone
Also, a better description of how to reproduce:
- double tap a cell and hold the second answer to the second
- move element up or down
Posted on Apple forums and posted a bug to Apple. Here is an example project that I presented to them (in which I was able to reproduce the indicated problem):
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableData = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",
@"One",@"Two",@"Three",@"Four",@"Five",
@"One",@"Two",@"Three",@"Four",@"Five",
@"One",@"Two",@"Three",@"Four",@"Five",
@"One",@"Two",@"Three",@"Four",@"Five",
@"One",@"Two",@"Three",@"Four",@"Five", nil];
self.tableView.editing = true;
[self.tableView setTableFooterView:[UIView new]];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.tableView.rowHeight = 20.0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"ScrollingDisplayCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if( cell == nil )
{
cell = [[[UITableViewCellNoPadding alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; // MEMCHK: Autorelease so no ownership implications
cell.textLabel.font = [UIFont fontWithName:@"Courier New" size:17.0];
cell.textLabel.frame = cell.contentView.bounds;
}
// Configure the cell...
cell.textLabel.text = [self.tableData objectAtIndex:indexPath.row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSString *stringToMove = self.tableData[sourceIndexPath.row];
[self.tableData removeObjectAtIndex:sourceIndexPath.row];
[self.tableData insertObject:stringToMove atIndex:destinationIndexPath.row];
}
And here is UITableViewCellNoPadding: UITableViewCell:
// https://stackoverflow.com/questions/3467288/center-align-text-in-uitableviewcell-problem
-(void)layoutSubviews
{
[super layoutSubviews];
self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.frame.size.width, self.textLabel.frame.size.height);
}
NOTE. It was more difficult to reproduce here than in my own application.
source to share
Since you indicated that users should not delete rows, I added editingStyleForRowAtIndexPath
to always return UITableViewCellEditingStyleNone
. Without this implementation, the default is returned UITableViewCellEditingStyleDelete
.
With the implementation posted below everything looks good and I have verified that all fixes are successfully applied to the table model.
Hope this helps!
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableModel = [NSMutableArray new];
for (int i = 0; i < 30; i++) {
[self addTableObject];
}
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.editing = true;
}
- (void) addTableObject
{
NSString *rowValue = [NSString stringWithFormat:@"%i", _tableModel.count+1];
[_tableModel addObject:rowValue];
}
#pragma mark - Table view delegate methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [_tableModel objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _tableModel.count;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//Make change in data model
id selectedValue = [_tableModel objectAtIndex:sourceIndexPath.row];
[_tableModel removeObjectAtIndex:sourceIndexPath.row];
[_tableModel insertObject:selectedValue atIndex:destinationIndexPath.row];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
source to share