在我們處理網(wǎng)絡(luò)請(qǐng)求時(shí),請(qǐng)求后經(jīng)常用到的是:
[tableView reloadData];
這樣是刷新整個(gè)tableView.今天寫的東西,用來刷新指定的cell
一: 創(chuàng)建tableView
- 1 創(chuàng)建UITableView
UITableView *machineTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 200); style:UITableViewStylePlain];
self.machineTable = machineTable;
- 2 初始化tableView, CJTableViewCell 為自定義的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CJTableViewCell = @"CJTableViewCell";
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"CJTableViewCell" owner:self options:nil] lastObject];
}
return cell;
}
二: 刷新指定的cell
- 1 獲取indexPath
NSArray *visiblePaths = [self.machineTable indexPathsForVisibleRows];
- 2 遍歷,并刷新指定cell
for (NSIndexPath *indexPath in visiblePaths)
{
//獲取到的indexpath為屏幕上的cell的indexpath
if (indexPath.row == 0)
{
self.indexP = indexPath;
// 獲取指定位置的cell
CJTableViewCell *cell0 = [self.machineTable cellForRowAtIndexPath:indexPath];
// 刷新
[self.machineTable reloadRowsAtIndexPaths:[NSArray arrayWithObjects:self.indexP,nil] withRowAnimation:UITableViewRowAnimationNone];
}
}