我想在为单元格中的按钮设置标签方面寻求一些帮助。这是一个链接到我之前发布的问题:iOS Using NSDictionary to load data into section and rows
然而,虽然我现在可以动态地传递数据,但我在每一行上的续订按钮似乎无法获取数据,并且只会在选择该部分中的任何行时检测到每个部分的相同书名。
根据我到目前为止所阅读的内容,这是因为按钮正在被回收,因此无法检测到正确选择了哪本书。我试图设置标签:
cell.renewButton.tag = indexPath.row;
我的代码现在的样子:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UserCustomCell *cell = (UserCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"UserCustomCell" owner:self options:nil];
cell = userCustomCell;
self.userCustomCell = nil;
cell.renewButton.tag = indexPath.row;
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);
cell.renewButton.frame = CGRectMake(600, 14, 68, 24);
}
[cell.renewButton useBlackActionSheetStyle];
//########## 编辑从这里开始 dataSource = [[NSMutableDictionary alloc] init]; // 这需要是一个 ivar
for (NSDictionary *rawItem in myArray) {
NSString *date = [rawItem objectForKey:@"date"]; // Store in the dictionary using the data as the key
NSMutableArray *section = [dataSource objectForKey:date]; // Grab the section that corresponds to the date
if (!section) { // If there is no section then create one and add it to the dataSource
section = [[NSMutableArray alloc] init];
[dataSource setObject:section forKey:date];
}
[section addObject:rawItem]; // add your object
}
self.dataSource = dataSource;
//NSLog(@"Data Source Dictionary: %@", dataSource);
NSArray *sections =[[dataSource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString *sectionTitle = [sections objectAtIndex:indexPath.section];
NSArray *items = [dataSource objectForKey:sectionTitle];
NSDictionary *dict = [items objectAtIndex:indexPath.row];
cell.bookTitle.text = [dict objectForKey:@"name"];
cell.detail.text = [NSString stringWithFormat:@"Due Date: %@ Due Time: %@",
[dict objectForKey:@"date"], [dict objectForKey:@"time"]];
cell.renewButton.tag = indexPath.row;
return cell;
}
但它根本不起作用。非常感谢任何建议:)谢谢!
PS:我的 xcode 副本没有更新,只更新到 version4。看到有人提到在 DataModel 中存储标签状态,但它仅在较新版本中可用。:)