【发布时间】:2013-12-03 04:24:04
【问题描述】:
我在 TableView 中使用了一个自定义 UITableViewCell,但我希望每个单元格之间有间距。放入单元格的信息是从我的数据库中获取的(我正在使用 Parse),所以每次都不同。我在这里找到了这个很棒的 SO 解决方案:Spacing between cells on UITableView with Custom UITableViewCell
但是在我实现该问题的代码后的问题是,现在我的表格视图只显示其他所有单元格,它跳过了我的数组中的信息并且不显示它。
这是我的代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 1)
return 40;
return 73;
//return 73;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
//Begin
static NSString *CELL_ID2 = @"SOME_STUPID_ID2";
// even rows will be invisible
if (indexPath.row % 2 == 1)
{
UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];
if (cell2 == nil)
{
cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CELL_ID2];
[cell2.contentView setAlpha:0];
[cell2 setUserInteractionEnabled:NO]; // prevent selection and other stuff
}
return cell2;
}
//End
static NSString *CellIdentifier = @"debateCell";
RipDebateTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RipDebateTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// Configure the cell
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//[df setLocale:enUSPOSIXLocale];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:@"MMM dd, yyyy "];
NSString *my = [df stringFromDate:[object objectForKey:@"Date"]];
NSLog(@"%@", my);
tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
cell.titleOf.text = [object objectForKey:@"Topic"];
cell.dateOf.text = [NSString stringWithFormat:@"Date: %@", my];
cell.typeOf.text = @"Debate";
cell.cellTop.image = [UIImage imageNamed:@"table-top"];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
self.objects 是来自数据库的信息进入的数组。怎样才能使单元格之间仍有间距,但数组中没有信息被跳过?
编辑:这是获取数据的代码
- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
//query whereKey:@"school" equalTo:[PFUser curr]
[query whereKey:@"school" equalTo:[[PFUser currentUser]objectForKey:@"mySchool"]];
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
if ([self.objects count] == 0) {
//[query whereKey:@"providerZipCode" equalTo:[NSNumber numberWithInt:78664]];
NSLog(@"NONE");
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
[query orderByAscending:@"Date"];
return query;
}
此查询将对象添加到 self.objects,一个数组。
如果仍然不清楚,这里是有关视图控制器的 Parse 文档的链接:https://parse.com/docs/ios_guide#ui-tables/iOS
【问题讨论】:
-
你只想要奇数行想要填充数据,其中偶数行留空..
-
对,偶数行是奇数行之间的边距,使它看起来像有间距。我的问题是我上面的代码没有显示我的对象数组中的所有内容......每次调用 cellForRowAtIndexPath 时,都会将一个 PFObject 发送到该方法中,因此当它是偶数行时,该对象被忽略并且没有任何反应它。
-
如你所知,此方法每行调用一次,这就是 tableview 中缺少数据的原因,正如 rdelmar 所说,你可以为每一行放置一个空的 headerview
-
尝试我发布的另一种方法
标签: iphone objective-c cocoa-touch uitableview