我有一个我在TableView中使用的自定义UITableViewCell,但我希望每个单元格之间有间距。从我的数据库中抓取放入单元格的信息(我正在使用Parse),所以每次都不同。我在这里找到了这个很棒的SO解决方案:Spacing between cells on UITableView with Custom UITableViewCell
但是我从该问题实现代码后的问题是,现在我的tableview只显示其他所有单元格,它会跳过我的数组中的信息并且不会显示它。
这是我的代码:
- (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,一个数组。
如果仍然不清楚,这里有关于视图控制器的解析文档的链接:https://parse.com/docs/ios_guide#ui-tables/iOS
答案 0 :(得分:0)
您可以返回数组计数的2倍(在numberOfRowsInSection中),并在cellForRowAtIndexPath中执行类似的操作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 1 ) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DataCell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[(indexPath.row -1)/2];
return cell;
}else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SpacerCell" forIndexPath:indexPath];
return cell;
}
}