gpt4 book ai didi

objective-c - UITableviewController - 更新特定单元格

转载 作者:行者123 更新时间:2023-12-04 16:37:46 25 4
gpt4 key购买 nike

我创建了带有按钮的自定义单元格,该按钮可将行的状态从“可用”更改为“已购买”,并且我还有一个显示状态的 UIImageview。当用户按下按钮时,我希望单元格以新状态重绘。

我已经能够在用户将单元格滚动出 View 后重绘 UIImage。这是我目前所拥有的:

初始化

- (void)viewDidLoad
{
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = YES;
self.contentSizeForViewInPopover = CGSizeMake(600.0, 560.0);
//[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.tableView setRowHeight:60];
[self.tableView setBackgroundColor:[UIColor blackColor]];
}

只有一个部分:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

所有项目都在“paketListe”数组中:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [paketListe count];
}

cellForRow 函数仅在单元格不在 View 中以及最初绘制单元格时被调用。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"CellForRow called!");
PaketTableViewCell *cell = [[[PaketTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil] autorelease];
[cell setDelegate:self];
Paket *tempPaket = [paketListe objectAtIndex:[indexPath row]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *userDocumentsPath = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/%@/%@",userDocumentsPath, [tempPaket productID], [tempPaket previewPic]];
[cell setProductIndex:[indexPath row]];
//... Setting some Attributes in my custom cell, cut out for simplicity....
cell.backgroundColor = [UIColor clearColor];
return cell;
}

Button 的代码,这是一个委托(delegate)调用,所以我的自定义销售调用 [delegate buttonPushed:(int)];

- (void) buttonPushed:(int) myProductIndex {
NSLog(@"Pushed: %@", [[paketListe objectAtIndex:myProductIndex] productID]);
CoreDataWrapper *tempCDW = [[CoreDataWrapper alloc] init];
[tempCDW initCoreData];
Paket *tempPaket = [paketListe objectAtIndex:myProductIndex];
//Doing some work in Core Data an my "paketListe" array. Left out for simplicity...
[tempCDW release];
//Here it begins....
[[self tableView] reloadData];
[[self tableView] beginUpdates];
[[self tableView] reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:myProductIndex inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[[self tableView] endUpdates];
}

现在我尝试使用不同的命令重新加载...仅使用 reloadDatabegin-/endUpdate 重新加载整个内容。没有什么可以刷新单元格。经过几个小时的阅读,我认为我做的一切都正确,但没有刷新。

感谢任何帮助。

最佳答案

快速而肮脏:

- (void)configureCell:(PaketTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

//Any cell-specific code

[cell setNeedsLayout]; //or [cell setNeedsDisplay];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//Code to create any/a general cell

[self configureCell:cell];

return cell;
}

- (void) buttonPushed:(int) myProductIndex {

//Code to identify the cell in which your button is (use "super")

[self configureCell:cellForPushedButton];
}

关于objective-c - UITableviewController - 更新特定单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8476778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com