目标C-仅从超级视图中删除UIButtons



我对objective-c还很陌生,遇到了一个很难解决的问题。。

我正在使用ShinobiDataGrid和他们的prepareCellForDisplay。

为了显示文本,我会这样做:

if([cell.coordinate.column.title isEqualToString:@"Task"]){
textCell.textField.textAlignment = NSTextAlignmentLeft;
textCell.textField.text = cellDataObj.task;
}

但对于一个特定的单元格,我正在尝试添加一个自定义按钮:

if([cell.coordinate.column.title isEqualToString:@"selected"]){

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 10 , 32, 32);

if(cellDataObj.selected){
[button setImage:[UIImage imageNamed:@"checked-box.png"] forState:UIControlStateNormal];
}else{
[button setImage:[UIImage imageNamed:@"unchecked-box.png"] forState:UIControlStateNormal];
}
button.tag = cell.coordinate.row.rowIndex;
[button addTarget:self action:@selector(CheckBoxPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
[button removeFromSuperview];
}

当我第一次运行应用程序时,复选框出现了。但当我重新加载我的ShinobiDataGrid时,复选框再次出现。我试着从超级视图中删除按钮,但没有成功。。。有什么建议吗?当我第三次重新加载我的ShinobiDataGrid时,复选框第三次不会出现,只是在我第二次重新加载ShinobiDataGrid时添加了另一个复选框。以下是整个方法:

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid prepareCellForDisplay:(SDataGridCell *)cell
{
SDataGridTextCell* textCell = (SDataGridTextCell*)cell;
CellData *cellDataObj = [cellHolderDisplay objectAtIndex:cell.coordinate.row.rowIndex];
textCell.textField.textAlignment = NSTextAlignmentCenter;
if([cell.coordinate.column.title isEqualToString:@"selected"]){

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 10 , 32, 32);

if(cellDataObj.selected){
[button setImage:[UIImage imageNamed:@"checked-box.png"] forState:UIControlStateNormal];
}else{
[button setImage:[UIImage imageNamed:@"unchecked-box.png"] forState:UIControlStateNormal];
}
button.tag = cell.coordinate.row.rowIndex;
[button addTarget:self action:@selector(CheckBoxPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
[button removeFromSuperview];
}

if([cell.coordinate.column.title isEqualToString:@"Task"]){
textCell.textField.textAlignment = NSTextAlignmentLeft;
textCell.textField.text = cellDataObj.task;
}
if([cell.coordinate.column.title isEqualToString:@"BLine. Start"]){
textCell.textField.text = cellDataObj.baselineDate;
}
}

有什么建议吗?

以下是CheckBoxPressed方法:

- (void)CheckBoxPressed:(UIButton *)sender
{
CellData *cell = [cellHolder objectAtIndex:sender.tag];
if([cell selected] == YES)
{
[[cell actualDate]setString:@""];
[[cell finishedDate] setString:@""];
[cell setSelected:NO];
}
else
{
if(([[cell actualDate] isEqualToString:@""]) && ([[cell finishedDate] isEqualToString:@""]))
{
[[cell actualDate]setString:[NSString stringWithFormat:@"%@ 8:00:00 AM",[self SetSpecialDateFormat:[NSDate date]]]];
[[cell finishedDate]setString:[NSString stringWithFormat:@"%@ 4:00:00 PM",[self SetSpecialDateFormat:[NSDate date]]]];
}
//actual date not populated but finish date populated
else if(([[cell actualDate]isEqualToString:@""]) && !([[cell finishedDate] isEqualToString:@""]))
{
[[cell actualDate]setString:[cell finishedDate]];
}
//finish date not populated but actual date populated
else if(!([[cell actualDate] isEqualToString:@""]) && ([[cell finishedDate] isEqualToString:@""]))
{
[[cell finishedDate]setString:[cell actualDate]];
}
[cell setSelected:YES];
}
[self UpdateEdittedCells:cell];
[self SetDisplayHolder];
//refresh grid
[gridReference reload];
}

您遇到的问题是shinobi数据网格会重新使用单元格,以优化内存使用率。因此,在单元格中添加一个按钮意味着,当单元格被重用时(当网格被重新加载或滚动时),按钮就会出现在那里。

可以删除prepareCellForDisplay:开头单元格的内容,然后您将有一个空单元格,可以向其中添加按钮:

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid prepareCellForDisplay:(SDataGridCell *)cell
{
if([cell.coordinate.column.title isEqualToString:@"selected"]){
NSArray *cellSubviews = [cell.subviews copy];
for (UIView *subview in subviews) {
[subview removeFromSuperview];
}
// Now safe to add the button
}
}

然而,这不是最好的方法。

您最好为selected列创建一个自定义单元格子类。这个单元总是有一个按钮,因此可以更好地利用单元重用机制。

要做到这一点,请创建SDataGridCell的子类,并按照通常的方式将register与网格一起注册。然后,当您在prepareCellForDisplay:中获得回调时,您知道该类型将是您的自定义类型。

您可以在ShinobiDataGrid用户指南:中找到实现这一目标的具体说明

https://www.shinobicontrols.com/docs/ShinobiControls/ShinobiGrids/2.8.0/Standard/Normal/docs/markdown_files/DataGridUserGuide.html#How到:创建自定义单元格

您要查找的示例名为"创建自定义单元格",是页面底部"如何操作"的最后一个示例。

此示例附带了一个完整的工作示例,该示例位于您下载的dmg中的samples文件夹中。与示例的不同之处在于,它使用的是数据源助手。您的代码不是,但将数据源助手的populateCell方法转换为您习惯使用的prepareCell方法相当简单。

相关内容

  • 没有找到相关文章

最新更新