I tried to make simple UIPopover with grouped table view. Everything is fine but first row is empty and it's data is in the last row. Whats wrong with my code?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
static NSString *kCellTextField_ID = @"CellTextField_ID";
cell = [tableView dequeueReusableCellWithIdentifier:kCellTextField_ID];
if (cell == nil) {
// a new cell needs to be created
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellTextField_ID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
} else {
// a cell is be开发者_JS百科ing recycled, remove the old edit field (if it contains one of our tagged edit fields)
UIView *viewToCheck = nil;
viewToCheck = [cell.contentView viewWithTag:kViewTag];
if (viewToCheck) [viewToCheck removeFromSuperview];
}
if (section == 1 && row == 0) {
UITextView *textView = [[self.sisalto objectAtIndex: row] valueForKey:kViewKey];
[cell.contentView addSubview:textView];
} else {
UITextField *textField = [[self.sisalto objectAtIndex: row] valueForKey:kViewKey];
[cell.contentView addSubview:textField];
}
return cell;
}
I might have found the problem. Look at the following code.
if (section == 1 && row == 0) {
UITextView *textView = [[self.sisalto objectAtIndex: row] valueForKey:kViewKey];
[cell.contentView addSubview:textView];
}
section == 1
indicates that you have two sections and your setting the first item of the second section to the first item of the array. That might be why it shows up last in your table view.
I finally figured out what was the problem. It was this sentence:
if (section == 1 && row == 0){
UITextView *textView = [[self.sisalto objectAtIndex: row] valueForKey:kViewKey];
[cell.contentView addSubview:textView];
}
It set just created cell with first value in datasource (sisalto). Obliviously, it should set first value of section 2 in datasource. When this is corrected everything works just fine.
精彩评论