我对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];
}
答案 0 :(得分:1)
(这并不完全清楚你在这之后会产生什么样的影响,所以我假设你想要一个填充了复选框的列,可以根据复选框进行检查或取消选中到您的数据模型)
您遇到的问题是shinobi数据网格重新使用单元格,以优化内存使用情况。因此,向单元格添加按钮将意味着在重新使用单元格时(重新加载或滚动网格时)按钮将在那里。
您可以删除X-Amz-Server-Side-Encryption-Customer-Key-MD5
开头的单元格内容,然后您可以添加一个空单元格来添加按钮:
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
中收到回调时,您知道该类型将是您的自定义类型。
您可以在ShinobiDataGrid用户指南中找到有关实现此目的的具体说明:
您正在寻找的示例名为"创建自定义单元格",并且是页面底部的" How-tos"中的最后一个。
此示例附带了一个完整的工作示例,在您下载的dmg的samples文件夹中提供。与示例的不同之处在于它使用的是数据源帮助程序。您的代码不是,但将数据源帮助器的prepareCellForDisplay:
方法转换为您习惯使用的populateCell
方法非常简单。