gpt4 book ai didi

iphone - 如何在运行时将一行插入 iPhone 上的 UITableView?

转载 作者:行者123 更新时间:2023-11-29 11:16:53 24 4
gpt4 key购买 nike

我创建了一个按钮,该按钮具有在运行时向 UITableView 添加一行的操作,但我的代码无法正常工作。我可以看到该行向下动画,但它仅在我的 UITableView 行上显示为动画未添加。我可以在此代码中执行哪些操作来查看单元格中添加的行?

我有 4 个部分,我想为第 0 行的第 1 个部分和第 0 行的第 2 个部分添加行:

-(IBAction)add:(id)sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray* path = [NSArray arrayWithObject:indexPath];

// fill paths of insertion rows here
[self.mytableview beginUpdates];
[self.mytableview insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];
[self.mytableview deleteRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];
[self.mytableview endUpdates];
[self.mytableview reloadData];
}

#pragma mark -
#pragma mark Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSInteger rows;
if (section==0) {
rows = 4;
//return rowForSectionOne;
//rows=rowForSectionOne++;
}
if (section == 1)
{
rows = 1;
}

return rows;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [mytableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}

if ([indexPath row] == 0 && [indexPath section] == 0)
{
cell.textLabel.text=@"Title";

cell.accessoryView = textField;
titlename=textField.text;
[[cell imageView] setImage:[UIImage imageNamed:@"DetailViewDue.png"]];
NSLog(@"******:%@",titlename);
}

if ([indexPath row] == 1 && [indexPath section] == 0)
{
cell.textLabel.text=@"Tags";
cell.detailTextLabel.text=app.Tags;
[[cell imageView] setImage:[UIImage imageNamed:@"DetailViewTag.png"]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

if ([indexPath row] == 2 && [indexPath section] == 0)
{
cell.textLabel.text=@"Notes";
cell.detailTextLabel.text=app.Notes;
[[cell imageView] setImage:[UIImage imageNamed:@"DetailViewNote.png"]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
fromDate = [[dateFormat stringFromDate:selectionData.fromDateSelected]retain];

if ([indexPath row] == 3 && [indexPath section] == 0)
{
cell.textLabel.text=@"DueDate";
cell.detailTextLabel.text=fromDate;
[[cell imageView] setImage:[UIImage imageNamed:@"DetailViewDue.png"]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}
if ([indexPath row] == 0 && [indexPath section] == 1)
{
cell.textLabel.text=@"Attach";

}

return cell;
}

最佳答案

你做得对。假设按下按钮时 -(IBAction)add:(id)sender 被调用。然后将 indexPath 与适当的 rowsection 组合起来。这里的部分是 0,1,2,3(因为你有 4 个部分 -

NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:1];
NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:0 inSection:2];
NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:0 inSection:3];
//put these indexpaths in a NSArray

[tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];

这应该会更新表格。无需在表格上执行 reloadData,因为您只添加一行(而不是更改整个表格)。还要确保 dataSource 每个部分都有这个新添加的条目,否则你的应用程序会崩溃

关于iphone - 如何在运行时将一行插入 iPhone 上的 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9075333/

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