我对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时,该复选框再次出现。我尝试从超级视图中删除按钮,但是那没用...有什么建议吗?第3次重新加载ShinobiDataGrid时,该复选框第3次未出现,仅当第2次重新加载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
的子类,并以与通常相同的方式在网格中注册。然后,当您在prepareCellForDisplay:
中获得回调时,您知道该类型将是您的自定义类型。您可以在ShinobiDataGrid用户指南中找到有关实现此目标的具体说明:
https://www.shinobicontrols.com/docs/ShinobiControls/ShinobiGrids/2.8.0/Standard/Normal/docs/markdown_files/DataGridUserGuide.html#How到:创建自定义单元格
您要查找的示例称为“创建自定义单元格”,它是“操作方法”的最后部分,就在页面底部。
此示例随附一个完整的工作示例,该示例在您下载的dmg的samples文件夹中提供。该示例的不同之处在于它使用的是数据源帮助器。您的代码不是,但是将数据源帮助程序的
populateCell
方法转换为您习惯使用的prepareCell
方法相当简单。关于ios - objective-c -从 super View 中仅删除UIButtons,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29867266/