UITableView didselectRowAtIndex 疑惑

发布于 2024-12-27 22:03:26 字数 1467 浏览 2 评论 0原文

哈,我正在做一个应用程序,它有一个与 Evernote 共享注释选项,我成功地完成了它,我有一个包含文本和一个用于将这些文本上传到 Evernote 的按钮的表格视图,如果用户单击该单元格,则会出现附加复选标记,他可以通过使用上传复选标记来选择诗句。但我的问题是,当用户单击一个单元格并点击上传按钮时,它将上传该行中的所有值,不仅是选定的行,而且是整行。 我的代码是 Buttonclick:

-(IBAction)sendNoteEvernote:(id)sender{

 NSMutableString *str = [[NSMutableString alloc] initWithString:@"NOTES:"];
    for (int i = 0; i<[appDelegate.notesArray count]; i++) {
        // UPLOAD STRINGS HERE
        if (selected[i])
        [str appendFormat:@"%@ ,",[appDelegate.notesArray objectAtIndex:i]];

        NSString * ENML= [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">\n<en-note>%@",str];
}

str为上传的值 selectrowatindexpath 中的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

 NSUInteger row = [indexPath row];
    selected[row] = !selected[row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = selected[row] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

in.hi declate BOOLEAN selected; 视图中已加载

for ( int i=0; i<[appDelegate.notesArray count]; i++) { 
    selected[i] = YES;
}

Ha ii ,am doing a application which has a share Note option to evernote i done it sucessfully,i have a tableview which contains text and a button for upload these text to evernote,if the user click the cell the accsocory checkmark is come and he can select the verse by using checkmark for uploade.but my problem is when the user click the one cell and tap the upload button it will upload all the values in the row not only the sectled row but also the entire row.
My code is
Buttonclick:

-(IBAction)sendNoteEvernote:(id)sender{

 NSMutableString *str = [[NSMutableString alloc] initWithString:@"NOTES:"];
    for (int i = 0; i<[appDelegate.notesArray count]; i++) {
        // UPLOAD STRINGS HERE
        if (selected[i])
        [str appendFormat:@"%@ ,",[appDelegate.notesArray objectAtIndex:i]];

        NSString * ENML= [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">\n<en-note>%@",str];
}

str is the value that is uploaded
code in selectrowatindexpath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

 NSUInteger row = [indexPath row];
    selected[row] = !selected[row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = selected[row] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

in.h i declate BOOLEAN selected;
in viewdidload

for ( int i=0; i<[appDelegate.notesArray count]; i++) { 
    selected[i] = YES;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌味 2025-01-03 22:03:26

我建议使用两条线索进行调试。首先,selected 的默认值为 YES,因此它假设默认选择所有行。其次,由于您正在查看点击事件,所以切换代码:

selected[row] = !selected[row];

还不够,您需要循环遍历数组,将所有内容设置为“否”,除了设置为“是”的indexPath.row。

for ( int i=0; i<[appDelegate.notesArray count]; i++) {
    if (i == indexPath.row) selected[i] = YES; else selected[i] = NO;
}

I would suggest two leads for debugging. First your default values of selected are YES, so it assumes all rows are by default selected. Second, since you're looking at the tapping event, the toggle code :

selected[row] = !selected[row];

is not enough, you need to loop through the array, set everything to NO, except the indexPath.row that you set to YES.

for ( int i=0; i<[appDelegate.notesArray count]; i++) {
    if (i == indexPath.row) selected[i] = YES; else selected[i] = NO;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文