iOS - 如何更新表格特定单元格中的信息(Objective-C)



>UPDATE

好的,所以我从 mysql 数据库中提取信息,然后将其显示在一个表中,其中每个单元格都包含与 mysql 数据库中的信息相对应的特定票证的信息。

但是,在另一个函数中,我更新了某个票证的信息(在数据库中(,并希望相应地更新保存票证信息的单元格。

主要问题是我无法单独调用每个单元格,因此我可以更新单元格中的信息,而不必更新(重新加载(所有单元格。

所以说我更新了票证#2的信息。我想要一种方法,我可以调用单元格 #2 并仅更新该单元格的信息。


所以,我目前正在像这样创建我的单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
int count = 0;
static NSString *CellIdentifier1 = @"Cell1";
cell *cell1 = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(cell1 == nil){
cell1 = [[cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
static NSString *CellIdentifier = @"Cell";
HelpDeskCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
items = [[NSArray alloc] initWithObjects:@"Cleanliness", @"Door", @"Peek Hole", @"Sink", @"Towel Rack", @"Closet", @"Carpet", @"Wall", @"Bed", @"Matress", @"Mattress Cover", @"Fridge", @"Blinds", @"Window", @"Screen", @"Air Conditioning", @"Chair", @"Desk", @"Garbage bin", @"Shelves", @"Phone", @"Jacks", @"Lights", @"Smoke Detector", @"Heat Detector", @"Light bulb", @"Deep Cleaning", @"Final Prep", nil];

if (!indexPath.row)
{
count = 1;
// first row
NSString *title;
if (self.filterClass.orderedBy == 1) {
title = [NSString stringWithFormat:@"Room: %d   (%d)", indexPath.section + 1 ,myArray[indexPath.section]];
}
else {
title = [NSString stringWithFormat:@"%@ (%d)", items[indexPath.section],myArray[indexPath.section]];
}
[cell1.titleLabel setFont:[UIFont systemFontOfSize:18]];
[cell1.titleLabel setText:title];
if (myArray[indexPath.section] > 0) {
[cell1.canOpen setFont:[UIFont systemFontOfSize:18]];
[cell1.canOpen setText:@">"];
}
else {
[cell1.canOpen setFont:[UIFont systemFontOfSize:18]];
[cell1.canOpen setText:@""];
}
if ([expandedSections containsIndex:indexPath.section])
{
}
else
{
}
}
else
{
int row = indexPath.row - 1;
int section = indexPath.section;
if(cell == nil){
cell = [[HelpDeskCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
CGFloat fontsize = 16;
if([[[[multiArray objectAtIndex:section] objectAtIndex:row]
objectForKey:@"priority"] isEqualToString:@"Critical"]){
[cell.IDLabel setBackgroundColor:[UIColor colorWithRed:1 green:0.5 blue:0.5 alpha:1.0]];
}
else{
[cell.IDLabel setBackgroundColor:[UIColor colorWithRed:0.925 green:0.925 blue:0.925 alpha:1.0]];
}
[cell.IDLabel setFont:[UIFont boldSystemFontOfSize:fontsize]];

//reduce fontsize to 12 for the information labels
//same on all devices and orientations
fontsize = 12;
//ticket status label
[cell.statusLabel setFont:[UIFont boldSystemFontOfSize:fontsize]];
[cell.statusLabel setTextColor:[UIColor whiteColor]];

//ticket category label
[cell.categoryLabel setBackgroundColor:[UIColor grayColor]];
[cell.categoryLabel setFont:[UIFont boldSystemFontOfSize:fontsize]];
[cell.categoryLabel setTextColor:[UIColor whiteColor]];

//ticket title label
[cell.titleLabel setFont:[UIFont boldSystemFontOfSize:fontsize]];
[cell.titleLabel setTextColor:[UIColor blackColor]];
//Label holds the user that submitted the ticket
[cell.submittedLabel setFont:[UIFont systemFontOfSize:fontsize]];
[cell.submittedLabel setTextColor:[UIColor blackColor]];
//Label holds the user currently working on the ticket
[cell.handleLabel setFont:[UIFont systemFontOfSize:fontsize]];
[cell.handleLabel setTextColor:[UIColor blackColor]];
//ticket date label
[cell.dateLabel setFont:[UIFont systemFontOfSize:fontsize]];

// Set the text of the subviews
NSString * ticketIdStr = [[[multiArray objectAtIndex:section] objectAtIndex:row] objectForKey:@"ticket_id"];
[cell.IDLabel setText:ticketIdStr];
NSString * ticketStatusStr = [[[multiArray objectAtIndex:section] objectAtIndex:row] objectForKey:@"status"];
[cell.statusLabel setText:ticketStatusStr];
if([ticketStatusStr isEqualToString:@"Open"]) {
[cell.statusLabel setBackgroundColor: [UIColor colorWithRed:0 green:0.6 blue:0.8 alpha:1.0]];
}
else if ([ticketStatusStr isEqualToString:@"In Progress"]) {
[cell.statusLabel setBackgroundColor: [UIColor colorWithRed:1.0 green:0.733 blue:0.2 alpha:1.0]];
}
else if ([ticketStatusStr isEqualToString:@"Resolved"]) {
[cell.statusLabel setBackgroundColor: [UIColor colorWithRed:0.6 green:0.8 blue:0 alpha:1.0]];
}
else if ([ticketStatusStr isEqualToString:@"Closed"]) {
[cell.statusLabel setBackgroundColor: [UIColor colorWithRed:0.8 green:0 blue:0 alpha:1.0]];
}
NSString * categoryStr = [[[multiArray objectAtIndex:section] objectAtIndex:row]objectForKey:@"category"];
[cell.categoryLabel setText:categoryStr];
NSString * titleStr = [[[multiArray objectAtIndex:section] objectAtIndex:row]objectForKey:@"title"];
NSString * userIDStr = [[[multiArray objectAtIndex:section] objectAtIndex:row]objectForKey:@"user_id"];
NSString * handledByStr = [[[multiArray objectAtIndex:section] objectAtIndex:row] objectForKey:@"handled_by"];
[cell.titleLabel setText: [NSString stringWithFormat:@"Title: %@", titleStr]];
[cell.submittedLabel setText: [NSString stringWithFormat:@"Submitted By: %@", userIDStr]];
[cell.handleLabel setText: [NSString stringWithFormat:@"Handled By: %@", handledByStr]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *orignalDate = [dateFormatter dateFromString:[[[[multiArray objectAtIndex:section] objectAtIndex:row]objectForKey:@"date_submitted"] substringToIndex:10]];
[dateFormatter setDateFormat:@"MMMM dd, yyyy"];
NSString * ticketDateStr = [dateFormatter stringFromDate:orignalDate];
[cell.dateLabel setText:ticketDateStr];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
if (count == 1) {
return cell1;
}
return cell;
}

我为每个单元格加载一个笔尖,然后更新那里的信息。例:

[cell.statusLabel setFont:[UIFont boldSystemFontOfSize:fontsize]];
[cell.statusLabel setTextColor:[UIColor whiteColor]];

但是,我很难调用每个单元格,然后从另一个函数更新它。这可能吗?

我认为你需要它来称呼它

HelpDeskCell *cell = (HelpDeskCell *)[self.tableView cellForRowAtIndexPath:#someIndexPath#];

在您的其他功能中。假设您知道要更新哪个票证/单元格,然后致电

cell.statusLabel.text = @"Hello World";

或您要进行的任何其他更新

最新更新