小编Ker*_*rrM的帖子

以编程方式将单元格添加到UITableView

我刚刚开始为iPhone编程,我正在创建一个连接到数据库并获取一组行名称并显示它们的应用程序.选择后,行背景颜色会发生变化,即您可以进行多项选择,它们都将是不同的颜色.所以我从服务器返回XML没问题,我创建了一个UITableView显示单元格.但是,我不知道如何将单元格添加到表格中.我看了一下,insertRowsAtIndexPaths但我不确定如何使用它?据我了解,insertRowsAtIndexPaths需要两个参数:

一个NSArray,包含单元格应该在哪个行以及在哪个部分.这个问题是我的应用程序将有一个动态的行数.如果我不知道我将拥有多少行,我将如何创建NSArray?我可以使用NSMutableArray吗?

它需要的第二个参数是动画 - 这非常简单.

我遇到的另一个问题是我在哪里创建单元格?你如何将细胞传递到tableview?

我试过阅读文档,但它似乎不太清楚!这是我在视图控制器的loadview方法中的代码:

 //Before this I get the XML from the server so I am ready to populate
 //cells and add them to the table view
 NSArray *cells = [NSArray arrayWithObjects:
                   [NSIndexPath indexPathForRow:0 inSection:0],
                   [NSIndexPath indexPathForRow:1 inSection:0],
                   [NSIndexPath indexPathForRow:2 inSection:0],
                   [NSIndexPath indexPathForRow:3 inSection:0],
                   [NSIndexPath indexPathForRow:4 inSection:0],
                   [NSIndexPath indexPathForRow:5 inSection:0],
                   [NSIndexPath indexPathForRow:6 inSection:0],
                   [NSIndexPath indexPathForRow:7 inSection:0],
                   [NSIndexPath indexPathForRow:8 inSection:0],
                   [NSIndexPath indexPathForRow:9 inSection:0],
                   [NSIndexPath indexPathForRow:10 inSection:0],
                   [NSIndexPath indexPathForRow:11 inSection:0],
                   [NSIndexPath indexPathForRow:12 inSection:0], …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c uitableview ios

12
推荐指数
2
解决办法
6万
查看次数

绑定到 SwiftUI 中的只读属性

我有一个模型类型,如下所示:

enum State {
    case loading
    case loaded([String])
    case failed(Error)

    var strings: [String]? {
        switch self {
        case .loaded(let strings): return strings
        default: return nil
        }
    }
}

class MyApi: ObservableObject {
    private(set) var state: State = .loading

    func fetch() {
        ... some time later ...
        self.state = .loaded(["Hello", "World"])
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试用它来驱动 SwiftUI 视图。

struct WordListView: View {
    @EnvironmentObject var api: MyApi

    var body: some View {
        ZStack {
            List($api.state.strings) {
                Text($0)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

正是在这里,我的假设失败了。List我试图获取加载时要在 …

swift swiftui combine

9
推荐指数
1
解决办法
8409
查看次数

NSDictionary allKeys - 它总是返回相同的顺序吗?

我是Objective-C的新手,今天我正在使用NSDictionaries并遇到了allKeys方法.据我所知,它以随机顺序返回包含字典键的NSArray.但是,这个订单总是一样吗?即,如果我连续20次在同一个字典上调用allKeys,我保证得到相同的结果顺序?

谢谢,

iphone xcode cocoa-touch objective-c nsdictionary

8
推荐指数
1
解决办法
5409
查看次数

iOS中的多线程 - 如何强制线程等待条件?

我正在创建一个从数据库中获取一组结果的应用程序 - 我使用MBProgressHUD来显示带动画的查询进度.我使用的方法在另一个线程中执行方法时调用动画,一旦完成,它就会隐藏动画.我的问题是,在致电:

[HUD showWhileExecuting:@selector(getResults) onTarget:self withObject:nil animated:YES];
Run Code Online (Sandbox Code Playgroud)

我想,如果没有结果,则显示一条警告,说明这一点,如果有,则加载下一个视图.到目前为止,我有这个代码:

[HUD showWhileExecuting:@selector(getResults) onTarget:self withObject:nil animated:YES];

if(self.thereAreEvents) {
    [self performSegueWithIdentifier:@"searchResults" sender:self];
} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No results" message:@"Sorry, there are no results for your search. Please try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}
Run Code Online (Sandbox Code Playgroud)

self.thereAreEventsgetResults方法结束时设置.但是,由于该方法在另一个线程中被调用,因此即使数据库中存在事件,该执行行也会继续并显示警报.

所以,从这里开始,我有两个问题:在iOS中实现等待信号机制的最简单方法是什么?在iOS中实现这种机制的最有效方法是什么?

谢谢!

iphone multithreading cocoa-touch ios

6
推荐指数
2
解决办法
1万
查看次数

SwiftUI 在详细视图中覆盖导航栏外观

我有一个超级简单的 SwiftUI 主从应用程序:

import SwiftUI

struct ContentView: View {
    @State private var imageNames = [String]()

    var body: some View {
        NavigationView {
            MasterView(imageNames: $imageNames)
                .navigationBarTitle(Text("Master"))
                .navigationBarItems(
                    leading: EditButton(),
                    trailing: Button(
                        action: {
                            withAnimation {
                                // simplified for example
                                self.imageNames.insert("image", at: 0)
                            }
                        }
                    ) {
                        Image(systemName: "plus")
                    }
                )
        }
    }
}

struct MasterView: View {
    @Binding var imageNames: [String]

    var body: some View {
        List {
            ForEach(imageNames, id: \.self) { imageName in
                NavigationLink(
                    destination: DetailView(selectedImageName: imageName)
                ) {
                    Text(imageName) …
Run Code Online (Sandbox Code Playgroud)

swiftui uinavigationbarappearance swiftui-navigationlink

6
推荐指数
1
解决办法
1803
查看次数

通过表单将文件上传到Drupal 7后,如何获取文件的路径?

我有一个表单,将文件上传到我的Drupal安装.我想在表中存储该文件的路径.如何获取最近上传的文件的路径?我试过了

$f = file_load($form_state['values']['field_file']);
$f->uri;
Run Code Online (Sandbox Code Playgroud)

但那不起作用.有线索吗?

php drupal fileapi

5
推荐指数
1
解决办法
6111
查看次数

主题($ hook,$ variables = array())以及它在Drupal 7中的工作原理

我正在尝试将主题内容输出到页面,我一直在尝试阅读theme()函数及其工作原理.据我了解,它提供了一种生成主题HTML的方法.这正是我想要的.现在,我不明白的是我如何传递HTML或我想要的变量,以便生成HTML.什么是$ hook参数?它是.tpl.php文件?如何构建此文件以便HTML显示在页面的内容部分?有人能用一种非常简单的方式解释theme()函数吗?

谢谢,

drupal

4
推荐指数
1
解决办法
8293
查看次数

滚动时选择UITableViewCell背景颜色更改

我有这些单元格,我已经设置了自定义背景颜色.当我选择单元格时,背景颜色很好,但是,当我向下滚动并向后滚动时,会发生两件事情:

  1. 如果选择的单元格不是很多,则选中时,视图外的单元格有时会返回默认的蓝色.

  2. 如果选择了大部分或全部细胞,那么熄灭的细胞将返回其中预先存在的细胞中的一种颜色 - 即.我选择所有单元格,向下滚动并向上翻转,顶部的单元格与底部的单元格颜色相同(或至少部分单元格 - 其他单元格保留自己的颜色).

这是我生成的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *row = [tableView cellForRowAtIndexPath:indexPath];
    UIView *backview = [[UIView alloc] initWithFrame:row.frame];
    backview.backgroundColor = [colours objectAtIndex:indexPath.row];
    row.selectedBackgroundView = backview;
}
Run Code Online (Sandbox Code Playgroud)

这就是所选择的细胞方法改变颜色的地方.单元格在这里创建:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"eventTypeID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSString *sEventType = [[self.eventTypes valueForKeyPath:@"name.text"] objectAtIndex:indexPath.row];
    cell.textLabel.text = sEventType;
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

每个单元格的颜色都在这里设置:

- (void)loadView {
    colours = …
Run Code Online (Sandbox Code Playgroud)

iphone scroll objective-c uitableview

3
推荐指数
1
解决办法
2881
查看次数

具有部分赋值的基于Rest Framework类的视图

我正在按照这里列出的教程为我的API创建基于类的通用视图 - 但是,我遇到了一个小问题.我想部分地更新视图背后的模型.我曾经能够partial在创建序列化程序时使用该属性来完成此操作.但是,似乎一旦我开始使用基于通用类的视图,我就失去了设置是否允许对模型进行部分更新的能力.如何覆盖partialModelSerializer 的属性?我的代码很简单:

class DejavuUserDetail(generics.RetrieveUpdateAPIView):
  '''
    Get a user or update a user
  '''
  lookup_field = "email"
  queryset = DejavuUser.objects.all()
  serializer_class = UserSerializer


class UserSerializer(serializers.ModelSerializer):
  class Meta:
    model = DejavuUser
    partial = True

  def restore_object(self, attrs, instance=None):
    """
    Given a dictionary of deserialized field values, either update
    an existing model instance, or create a new model instance.
    """
    if instance is not None:
      #set the required fields and return the instance
Run Code Online (Sandbox Code Playgroud)

我正在尝试通过PUT访问API

django django-rest-framework

2
推荐指数
1
解决办法
1702
查看次数

如何在Drupal中向表单元素添加id或类?

我要做的是在隐藏字段中添加一个id,以便我可以通过JS编辑它的值.例如,我想通过Drupal形式给出我创建的隐藏元素:

$form['position'] = array(
'#type' => 'hidden',
'#default_value' => '57.149953,-2.104053',
);
Run Code Online (Sandbox Code Playgroud)

哪个输出:

<input type="hidden" name="position" value="57.149953,-2.104053" />
Run Code Online (Sandbox Code Playgroud)

好吧,我想为该输入添加一个id,一个名称和一个类.这样做的最佳方法是什么?

谢谢

javascript forms drupal

1
推荐指数
1
解决办法
3612
查看次数